【问题标题】:show a usercontrol in wpf window by button通过按钮在 wpf 窗口中显示用户控件
【发布时间】:2014-07-12 20:48:25
【问题描述】:

我有用户控制并想通过单击 wpf 设计的窗口中的按钮来显示它。 我做了一个控制用户项目,在wpf项目中引用是这样的:

xmlns:myproj="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"

<Grid>tag 我有这个:

<myproj:UserControl1   Visibility="Hidden"
                       x:Name="customproj" />

正如我所说,我在 wpf 项目的主窗口中有一个按钮:

<Button Content="click me" HorizontalAlignment="Left" Margin="186,127,0,0" VerticalAlignment="Top" Width="124" Height="31" Click="Button_Click"/>

但是我不知道怎么写Button_Click打开控制用户的事件。

private void Button_Click(object sender, RoutedEventArgs e)
        {
            //I don't know what to write!!!
        }

我搜索了很多,但没有找到适合我的问题的答案!

谢谢

【问题讨论】:

    标签: c# wpf xaml visual-studio-2012 user-controls


    【解决方案1】:

    只需将 UserControl 的 Visibility 设置为 Visible

    private void Button_Click(object sender, RoutedEventArgs e)
    {
       customproj.Visibility = Visibility.Visible;
    }
    

    【讨论】:

    • 这是我的问题,我没有注意到我应该使用我在网格标签的定义标签中定义的用户控制器的名称
    • 这不是资源友好的。假设您的应用程序在运行时加载了许多用户控件,那么计算资源可能会非常繁重。在代码隐藏中使用 switch case 实例化这些用户控件的方法可能是首选
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多