<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Background="Red"></Button>
<Grid Grid.Column="1" Name="BlueGrid" Background="Blue" PreviewMouseMove="Grid_PreviewMouseMove"></Grid>
</Grid>
private void Grid_PreviewMouseMove(object sender, MouseEventArgs e)
{
var pos = Mouse.GetPosition(BlueGrid);
}
所以在这个窗口里有一个网格。每次鼠标在蓝色网格上时,我都要获得鼠标的位置。它工作得很好,但它只有一个问题。当你点击按钮(红色按钮),并按住它,你进入网格的事件不工作了。
我尝试了其他事件,如MouseMove,MouseEnter和DragEnter,但它们都不起作用。有没有什么我可以做的,以获得鼠标的位置,网格无论什么?
1条答案
按热度按时间sbdsn5lh1#
解释是当你点击一个
ButtonBase
时,控件会捕获鼠标。这意味着所有鼠标输入事件都使用ButtonBase
作为源,即使鼠标输入事件是在元素边界之外触发的。解决方案是在公共父容器上侦听路由的鼠标输入事件,并使用
CaptureMode.SubTree
控制鼠标捕获。您可以通过访问
RoutedEventArgs.Source
属性来标识感兴趣的事件源: