[英]adding User control / Page to windows xaml programmatically in WPF
我想在 Window1.xaml(主窗口)中以編程方式在我的兩個用戶控件 AddUsers.xaml(用戶控件)和 Viewusers.xaml(用戶控件)之間切換。
我正在嘗試通過 Window1.xaml 中的按鈕事件切換用戶控件。
我的 Window1。 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
添加用戶。 xml代碼:
<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>
首先,您需要為堆棧面板命名
<StackPanel Name="myStack" Grid.Row="1" >
</StackPanel>
然后你需要類似的東西
public partial class MainWindow : Window
{
private addUser _addUser;
private viewUser _viewUser;
private Control _currentUser;
public MainWindow()
{
InitializeComponent();
_addUser = new addUser();
_viewUser = new viewUser();
_currentUser = _viewUser;
myStack.Children.Add(_currentUser);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
myStack.Children.Clear();
if (_currentUser == _addUser)
{
_currentUser = _viewUser;
}
else
{
_currentUser = _addUser;
}
myStack.Children.Add(_currentUser);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.