Wpf 如何在弹出窗口外单击时关闭弹出窗口

Wpf 如何在弹出窗口外单击时关闭弹出窗口,wpf,xaml,Wpf,Xaml,这里是XAML打开弹出窗口的代码已检查btnViewDetail,我需要在弹出窗口的外侧单击关闭弹出窗口 <Popup IsOpen="{Binding IsChecked, ElementName=btnViewDetail}" PopupAnimation="Fade" Width="300" Height="225" PlacementTarget="{Binding ElementName=svTotalStock}" Placement="Top" StaysOpen="Fals

这里是
XAML
打开弹出窗口的代码已检查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>

如果
StaysOpen
属性无法处理您的情况,则当容器元素(在您的情况下,
Grid
)具有
Focusable=“True”


必须指定两个属性:

StaysOpen=false

当StaysOpen属性设置为true时,弹出窗口将保持打开状态,直到通过将IsOpen属性设置为false显式关闭为止。当StaysOpen为false时,弹出控件将截获所有鼠标和键盘事件,以确定其中一个事件何时发生在弹出控件之外

接下来,设置弹出窗口的子项:
Focusable=false


然后,在您希望tp打开弹出窗口的其他用户控件中,在
UIElement.LostMouseCapture
上定义一个EventTrigger,以设置弹出窗口的
IsOpen=true

弹出窗口的属性
StaysOpen=false
执行此操作。

我怀疑弹出窗口打开的控件。我甚至遇到了同样的问题,并且通过鼠标点击目标按钮打开弹出窗口来重新获得了信任。

我想知道为什么
StaysOpen=“False”
不起作用?StaysOpen=“False”在调焦用户控件时起作用。
private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    gridContainer.Focus();
}