I'd like to know if there is an alternate way to display usercontrols
inside mainwindow
in WPF application.
Currently I make use of the visibility property of usercontrols
to display one usercontrol at a time on a button click. I set the visibility of the usercontrols to Hidden
and on button clicks I change the visibility. It works perfectly. But is this the correct way to do this?
EDIT:
I tried something like this but it does not work.
mainwindow.xaml:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="54*" />
<RowDefinition Height="257*" />
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ContentControl Content="{Binding CurrentView}"/>
</Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="25,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Right" Margin="0,12,251,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
There are two user controls namely-UserControl1 and UserControl2. In the code behind:
private UserControl currentview;
private UserControl CurrentView
{
get
{
return this.currentview;
}
set
{
this.currentview = value;
//RaisePropertyChanged("CurrentView");
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
CurrentView = uc1;
}
It does not work. What is the right way to do it?