通过按钮在 wpf 窗口中显示用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24717179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:08:27  来源:igfitidea点击:

show a usercontrol in wpf window by button

c#wpfxamlvisual-studio-2012user-controls

提问by Mojtaba Ahmadi

I have user control and wanna show it by clicking a button in a window designed by wpf. I made a control user project and referenced it in a wpf project in this way:

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

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

and in <Grid>tag I have this :

<Grid>标签中我有这个:

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

and as I told I have a button in the main window in wpf project:

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

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

but I don't know how to write the event of the Button_Clickto open the control user.

但是我不知道如何编写Button_Click打开控件用户的事件。

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

I searched a lot but didn't find a suitable answer for my problem!

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

thank you

谢谢你

回答by Rohit Vats

Just set the Visibilityof UserControl to Visible:

只需将VisibilityUserControl设置为Visible

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