我想做的是在按钮点击时更改/滑动wpf窗口的内容。我是wpf的新手,不知道如何做到这一点。如果有人可以帮助我,我将非常感激。最好提供任何视频教程。
我想做的是在按钮点击时更改/滑动wpf窗口的内容。我是wpf的新手,不知道如何做到这一点。如果有人可以帮助我,我将非常感激。最好提供任何视频教程。
您可以将窗口的内容放入UserControl中。然后,您的窗口只有一个内容控件和一个用于更改内容的按钮。单击按钮时,您可以重新分配内容控件的内容属性。
我为此制作了一个小例子。
您的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>
我已经向解决方案中添加了两个用户控件。 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();
}
}
更新:
我创建了一个叫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>
在上述按钮单击事件中,您可以将此用户控件的新实例分配给内容控件。您可以通过以下方式完成:
this.contentControl.Content = new MyUserControl();
new MyUserControl();
不同吗? - Assimilater