如何关闭 WPF 弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12018641/
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
How to close WPF popup window?
提问by Santosh
I am developing project in WPF and I am facing a problem using a popup window in my project. I use popup control in my window as shown below:-
我正在 WPF 中开发项目,但在项目中使用弹出窗口时遇到问题。我在我的窗口中使用弹出控件,如下所示:-
<Popup HorizontalAlignment="Center" VerticalAlignment="Center"
AllowsTransparency="True" x:Name="popup" Placement="Center"
OpacityMask="#FFC86E6E" Closed="popup_Closed" >
<Grid Height="auto" Width="auto" Margin="0" >
<Grid.RowDefinitions>
<RowDefinition Height="0.488*"/>
<RowDefinition Height="0.512*"/>
</Grid.RowDefinitions>
<Frame x:Name="popupframe" Margin="0" Grid.Row="1" />
<Button Width="30" Height="30" HorizontalAlignment="Right"
Margin="0,0,10,-50" VerticalAlignment="Center"
BorderThickness="0" BorderBrush="{x:Null}"
ClickMode="Press" Click="Button_Click"
Foreground="{x:Null}">
<Button.Background>
<ImageBrush ImageSource="Image/1329666144_button_cancel.png" Stretch="UniformToFill"/>
</Button.Background>
</Button>
</Grid>
</Popup>
Now i Create new page in wpf with textbox and button and set this page to popup frame show below:-
现在我使用文本框和按钮在 wpf 中创建新页面,并将此页面设置为弹出框显示如下:-
popupframe.Content=new SessionObjection();
Now i want to close popup window with page button. How i do...
现在我想用页面按钮关闭弹出窗口。我怎样做...
回答by Peter Lillevold
回答by Aghilas Yakoub
you can try with
你可以试试
private void btnClosePopup_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
回答by Tomato
If you would like to solve it in the XAML code, here is a working solution (put the Close button inside the popup):
如果您想在 XAML 代码中解决它,这里有一个可行的解决方案(将关闭按钮放在弹出窗口中):
<Button Name="CloseThisPopUp" VerticalAlignment="Top" HorizontalAlignment="Right" Content="X">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseThisPopUp" Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
You can also use this code to open a popup, just change the IsOpen property to "True".
您也可以使用此代码打开弹出窗口,只需将 IsOpen 属性更改为“True”即可。

