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

Sam*_*Sam 6 wpf xaml

这是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>
Run Code Online (Sandbox Code Playgroud)

Dan*_*nov 11

Popup的财产StaysOpen = false做这项工作.


小智 6

如果StaysOpen属性无法处理您的情况,则必须MouseDown在容器元素(在您的情况下Grid)中捕获窗口上的事件Focusable="True"

private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    gridContainer.Focus();
}
Run Code Online (Sandbox Code Playgroud)