WPF 获取鼠标下的元素
-
09-06-2019 - |
题
WPF 有没有办法在 MouseMove 事件上获取鼠标下的元素数组?
解决方案
从 ”WPF 释放”,第 383 页:
视觉命中测试可以告知您有关 全部
Visual
与位置相交的s,[...]您必须使用[...[VisualTreeHelper.]HitTest
方法接受一个HitTestResultCallback
代表。在此版本的HitTest
返回,每个相关的代表一次被调用一次Visual
, ,从最上方开始,结束于Bottommost。
这种回调的签名是
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;
}
欲了解更多信息,请咨询 MSDN 文档 VisualTreeHelper.HitTest
.
其他提示
您还可以尝试使用 Mouse.DirectlyOver 属性来获取鼠标下方的最顶层元素。
不隶属于 StackOverflow