在WPF中,有没有一种方法可以在MouseMove事件中获取鼠标下的元素数组?
在WPF中,有没有一种方法可以在MouseMove事件中获取鼠标下的元素数组?
您也可以尝试使用Mouse.DirectlyOver属性来获取鼠标下最上层的元素。
来自 "WPF Unleashed" 第383页:
可视化命中测试可以告诉您有关与位置相交的所有
Visual
,[...] 您必须使用 [...] 接受HitTestResultCallback
委托的[VisualTreeHelper.]HitTest
方法。在此版本的HitTest
返回之前,对于每个相关的Visual
,从上到下依次调用委托。
此类回调的签名为
HitTestResultBehavior Callback(HitTestResult result)
并且它必须返回HitTestResultBehaviour.Continue
以接收进一步的命中,如下所示(来自MSDN链接页面):
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
// Add the hit test result to the list that will be processed after the enumeration.
hitResultsList.Add(result.VisualHit);
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
}
如需更多信息,请查阅VisualTreeHelper.HitTest的MSDN文档。
Mouse.Capture(...)
和Mouse.PreviewMouseDownOutsideCapturedElementEvent
无法正常工作:如果一个元素拥有鼠标捕获,那么无论鼠标指针在哪里,它都被视为直接位于该元素上方。 - adereshMouse.Capture(...)
和Mouse.DirectlyOver
相关问题 - https://stackoverflow.com/questions/5399357/how-can-i-get-the-element-at-the-mouse-position-even-when-the-mouse-is-captured?lq=1 - aderesh