在 WPF 中以编程方式将用户控件/页面添加到 Windows xaml
我想在 Window1.xaml(主窗口)中以编程方式在两个用户控件 AddUsers.xaml(用户控件)和 Viewusers.xaml(用户控件)之间切换。
我正在尝试通过 Window1.xaml 中的按钮事件切换用户控件。
我的窗口1。 xaml 像这样
<Window x:Class="SwitchUsers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="400*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" >
<Button Content="Swithc User Control " Height="23" HorizontalAlignment="Right" Margin="0,40,284,0" Name="btnSwittch" VerticalAlignment="Center" Width="168" />
</StackPanel>
<StackPanel Grid.Row="1" >
<!--Here I want to display two user controls by switching from button on Top -->
</StackPanel>
</Grid>
我有两个用户控件 addUser.xaml 和 viewUser.xaml
addUser。 xaml 代码:
<UserControl x:Class="SwitchUsers.addUser"
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" Loaded="UserControl_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="29,79,0,0" Name="textBlock1" Text=" Enter Your Name" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="29,105,0,0" Name="textBlock2" Text="Enter Your Password" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,76,0,0" Name="txtBxName" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,105,0,0" Name="txtBxPassword" VerticalAlignment="Top" Width="120" />
<Button Content="Log In" Height="23" HorizontalAlignment="Left" Margin="171,160,0,0" Name="btnLogin" VerticalAlignment="Top" Width="93" />
</Grid>
</UserControl>
和我的第二个用户控件 viewUser.xaml
<UserControl x:Class="SwitchUsers.viewUser"
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">
<Grid>
<!-- I hidden all UI Controls here to keep code short -->
</Grid>
</UserControl>
I want to switch between my two user controls AddUsers.xaml (user control) and Viewusers.xaml(user Control) programmatically in Window1.xaml (main Window).
I'm trying to switch user controls by Button Event in Window1.xaml.
my Window1. xaml goes like this
<Window x:Class="SwitchUsers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="400*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" >
<Button Content="Swithc User Control " Height="23" HorizontalAlignment="Right" Margin="0,40,284,0" Name="btnSwittch" VerticalAlignment="Center" Width="168" />
</StackPanel>
<StackPanel Grid.Row="1" >
<!--Here I want to display two user controls by switching from button on Top -->
</StackPanel>
</Grid>
I have two User Controls addUser.xaml and viewUser.xaml
addUser. xaml code:
<UserControl x:Class="SwitchUsers.addUser"
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" Loaded="UserControl_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="29,79,0,0" Name="textBlock1" Text=" Enter Your Name" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="29,105,0,0" Name="textBlock2" Text="Enter Your Password" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,76,0,0" Name="txtBxName" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,105,0,0" Name="txtBxPassword" VerticalAlignment="Top" Width="120" />
<Button Content="Log In" Height="23" HorizontalAlignment="Left" Margin="171,160,0,0" Name="btnLogin" VerticalAlignment="Top" Width="93" />
</Grid>
</UserControl>
and my second user control viewUser.xaml
<UserControl x:Class="SwitchUsers.viewUser"
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">
<Grid>
<!-- I hidden all UI Controls here to keep code short -->
</Grid>
</UserControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先你需要给你的堆栈面板一个名称
然后你需要类似的东西
First you need to give your stack panel a name
Then you need something similar to