我有一个弹出窗口,StaysOpen=False
所以我想通过单击弹出窗口之外的任何位置来关闭它。在弹出窗口中,我有一个DataGrid
. 如果我打开弹出窗口然后单击其他地方,弹出窗口将被关闭。但是,如果在单击弹出窗口外部之前,我将单击中的列标题,则不会发生这种情况DataGrid
。测试 XAML:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black">
<Grid>
<ToggleButton x:Name="btn" VerticalAlignment="Top">Open</ToggleButton>
<Popup StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=btn}" >
<DataGrid Width="150" Height="150">
<DataGrid.Columns>
<DataGridTextColumn Header="Column" />
</DataGrid.Columns>
</DataGrid>
</Popup>
</Grid>
</Window>
我认为这是因为列标题在单击时捕获了鼠标,并且弹出窗口不再接收鼠标事件。我试图在LostMouseCapture
事件上添加一个处理程序,以便通过弹出窗口捕获鼠标,但它似乎并不那么容易。有任何想法吗?