我想知道在 WPF 应用程序中是否有另一种显示usercontrols
方式mainwindow
。目前,我利用 的可见性属性usercontrols
在单击按钮时一次显示一个用户控件。我将用户控件的可见性设置为Hidden
单击按钮并更改可见性。它完美地工作。但这是正确的方法吗?
编辑:
我尝试过这样的事情,但它不起作用。
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>
有两个用户控件,即-UserControl1 和UserControl2。在后面的代码中:
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;
}
这没用。正确的方法是什么?