wpf 在弹出窗口外单击时如何关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34718925/
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 the pop up window when I click outside of popup window
提问by Sam
Here is XAMLcode for open popup window IsChecked btnViewDetail, I need to close popup on click out side of popup window.
这是XAML打开弹出窗口 IsChecked btnViewDetail 的代码,我需要在弹出窗口的点击侧关闭弹出窗口。
<Popup IsOpen="{Binding IsChecked, ElementName=btnViewDetail}" PopupAnimation="Fade" Width="300" Height="225" PlacementTarget="{Binding ElementName=svTotalStock}" Placement="Top" StaysOpen="False">
<Grid Background="Black">
<TextBlock TextWrapping="Wrap" Text="Raw Materials details"
VerticalAlignment="Top" Height="25" FontFamily="Segoe UI Semibold"
Padding="7,6,0,0" FontWeight="Bold" FontSize="14" Foreground="White"
Margin="0,2,59,0"/>
<Border BorderThickness="1" BorderBrush="Black"/>
</Grid>
</Popup>
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem IsCheckable="True" Name="btnViewDetail" Header="View Details"/>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
回答by Danil Shaykhutdinov
property of Popup StaysOpen = falsedo this work.
Popup 的属性StaysOpen = false完成这项工作。
回答by Javad Ebrahimi
if StaysOpenproperty cannot handle your situation, you have to capture MouseDownevent on your Window when your container element(in your situation, Grid) has Focusable="True"
如果StaysOpen属性无法处理您的情况,则MouseDown当您的容器元素(在您的情况下Grid)具有时,您必须在 Window 上捕获事件Focusable="True"
private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
gridContainer.Focus();
}
回答by itzmebibin
There are two properties you have to assign:
您必须分配两个属性:
StaysOpen = false,
StaysOpen = false,
When the StaysOpen property is set to true, Popup stays open until it is explicitly closed by setting the IsOpen property to false. When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control.
当 StaysOpen 属性设置为 true 时,Popup 将保持打开状态,直到通过将 IsOpen 属性设置为 false 将其显式关闭。当 StaysOpen 为 false 时,Popup 控件会拦截所有鼠标和键盘事件,以确定这些事件之一何时发生在 Popup 控件之外。
Next, set popup's child : Focusable = false
接下来,设置弹出窗口的孩子: Focusable = false
Then in other User Control which u wants tp open the popup, define an EventTrigger on UIElement.LostMouseCaptureto set the popup's IsOpen = true;
然后在你想要打开弹出窗口的其他用户控件中,定义一个 EventTriggerUIElement.LostMouseCapture来设置弹出窗口的IsOpen = true;
回答by sahithi
I suspect the control from where the popup is opened. I even had same issue and reloved by opening the popup by MouseLeftButtonUp of the target.
我怀疑打开弹出窗口的控件。我什至遇到了同样的问题,并通过目标的 MouseLeftButtonUp 打开弹出窗口而深受喜爱。

