在 wpf 窗口中动态更改内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16855352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing content dynamically in wpf window
提问by Saad Khan
What i want to do is change/slide the content of a wpf window on the click of a button. I am new to wpf and have no clue how to do this. Please, if anyone can help me, I will be so grateful. Any video tutorials would be best.
我想要做的是单击按钮更改/滑动 wpf 窗口的内容。我是 wpf 的新手,不知道如何做到这一点。请,如果有人可以帮助我,我将不胜感激。最好有视频教程。
回答by Tomtom
You can put the content of the window into a UserControl. Your window then only has a content-control and a button to change the content. On a click on the button you can reassign the content-property of the content-control.
您可以将窗口的内容放入 UserControl。然后,您的窗口只有一个内容控件和一个用于更改内容的按钮。单击按钮,您可以重新分配内容控件的内容属性。
I've made a small example for this.
我为此做了一个小例子。
The XAML-Code for your MainWindow can look like this:
MainWindow 的 XAML 代码如下所示:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Switch" Click="ButtonClick"/>
<ContentControl x:Name="contentControl" Grid.Row="1"/>
</Grid>
</Window>
I've added two UserControls to the solution. The CodeBehind for the MainWindow looks like:
我在解决方案中添加了两个 UserControl。MainWindow 的 CodeBehind 如下所示:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.contentControl.Content = new UserControl1();
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
this.contentControl.Content = new UserControl2();
}
}
UpdateI've created a small usercontrol called MyUserControl. The xaml-markup looks like
更新我创建了一个名为 MyUserControl 的小用户控件。xaml 标记看起来像
<UserControl x:Class="WpfApplication.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
<Label Content="This is a label on my UserControl"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Testbutton 1" Margin="5"/>
<Button Content="Testbutton 2" Margin="5"/>
</StackPanel>
<CheckBox Content="Check Me"/>
</StackPanel>
</UserControl>
In the button-click-event above you can assign a new instance of this usercontrol to the content-control. This you can do by:
在上面的按钮单击事件中,您可以将此用户控件的新实例分配给内容控件。你可以这样做:
this.contentControl.Content = new MyUserControl();