【问题标题】:How do I switch the WPF UserControl?如何切换 WPF 用户控件?
【发布时间】:2014-09-11 08:02:42
【问题描述】:

当按下连接到主窗口的 UserControl1 中的按钮时,我想更改为 UserControl2。

MainWindow.xaml

<Viewbox Grid.Row="1" HorizontalAlignment="Center">
        <Grid>
            <local:A x:Name="a" Margin="0,0" Width="1200" /> <--UserControl1
            <local:B x:Name="b" Margin="0,0" Width="1200" /> <--UserControl2
        </Grid>
    </Viewbox>

MainWindow.xaml.cs

public MainWindow()
    {
        this.Closed += this.WindowClosed;
        this.InitializeComponent();

        this.a.Visibility = Visibility.Visible;
        this.b.Visibility = Visibility.Hidden;
    }

如果按钮被点击

this.a.Visibility = Visibility.Hidden; 
this.b.Visibility = Visibility.Visible;

我怎样才能做到这一点?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    将以下代码放入您的按钮单击事件中

    Window w = Window.GetWindow(this);
    if(null != w)
    {
        ((UserControl)w.FindName("a")).Visibility = Visibility.Hidden;
        ((UserControl)w.FindName("b")).Visibility = Visibility.Visible;
    }
    

    【讨论】:

    • 正确!我要它!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2020-01-27
    相关资源
    最近更新 更多