我在WPF表单中有一个StackPanel
。我确实有3个不同的WPF用户控件,我需要在面板内加载,具体取决于条件。任何想法,我知道它应该在后面的代码中完成,试图将usercontrol添加到stackPanel我无法找到add方法。我是否需要使用与堆栈面板不同的控制来执行此操作?
<StackPanel Panel.ZIndex="1" x:Name="pnlRightMenu" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,403,-576,342" Grid.Column="1" Width="576" VerticalAlignment="Center">
<Border BorderBrush="Transparent" BorderThickness="1" Width="550" Background="#4C808080" Margin="0,-163,0,-126" >
<Grid>
<Button x:Name="btnRightMenuHide" Click="btnRightMenuHide_Click" Content=">>" Margin="-16,-191,0,0" FontSize="10" RenderTransformOrigin="-1.338,2.571" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="Hidden"/>
</Grid>
</Border>
</StackPanel>
答案 0 :(得分:1)
您可以将任何UIElement
添加到StackPanel
的{{1}}集合中:
Children
答案 1 :(得分:1)
在按钮点击事件处理程序中,您将要切换条件。
使用开关将用户控件的新实例附加到StackPanel的Children集合中:
private void btnRightMenuHide_Click(object sender, RoutedEventArgs e)
{
switch (condition)
{
case "case 1":
UserControl1 uc1 = new UserControl1();
pnlRightMenu.Children.Add(uc1);
break;
case "case 2":
UserControl2 uc2 = new UserControl2();
pnlRightMenu.Children.Add(uc2);
break;
case "case 3":
UserControl3 uc3 = new UserControl3();
pnlRightMenu.Children.Add(uc3);
break;
}
}