如何关闭弹出用户控件

时间:2019-01-08 10:20:26

标签: c# wpf xaml

我想通过单击按钮来关闭用户控件。 UI

因此,单击按钮时,红色的用户控件需要关闭。 但是用我的代码关闭了整个应用程序。

这不起作用:

Window.GetWindow(this).Close(); 这也不起作用:

Window parentwin = Window.GetWindow(this); parentwin.Close(); 当我运行其中之一时,整个应用程序都会关闭...

因此,我想要的是特定的用户控件关闭了,但是项目没有关闭。

2 个答案:

答案 0 :(得分:0)

您可以使用Popup显示用户控件

Popup p = new Popup();
YourUserControl yuc = new YourUserControl();
p.Child = yuc;
p.IsOpen = true;

,您可以使用

将其关闭
p.IsOpen=false;

答案 1 :(得分:0)

您可以为此使用弹出控件。

    <CheckBox Name="CheckBoxIsOpen"  Content="IsOpen" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
    <Popup Name="myPopup" Width="300" Height="100" PlacementTarget="{Binding ElementName=CheckBoxIsOpen}" VerticalOffset="10" IsOpen="{Binding ElementName=CheckBoxIsOpen, Path=IsChecked}">
        <Border BorderBrush="Black" BorderThickness="1" Background="LightGray">
            <TextBlock Text="This is the content of my popup."/>
        </Border>
    </Popup>

然后,您可以通过设置IsOpen属性来显示或隐藏弹出窗口。

myPopup.IsOpen = true;