2008-09-05 81 views

回答

34

从 “WPF Unleashed”,383页:

视觉命中测试可以告诉你 约所有Visual s表示相交 位置,[...]你必须使用[...]接受代理的 [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; 
} 

欲了解更多信息,请查阅MSDN documentation for VisualTreeHelper.HitTest

+5

有关示例,请参见以下msdn链接(部分:使用命中测试结果回调)。 [http://msdn.microsoft.com/en-us/library/ms752097.aspx](http://msdn.microsoft.com/en-us/library/ms752097.aspx) – 2013-01-24 10:27:46

36

您也可以尝试使用Mouse.DirectlyOver属性获取是鼠标下的最顶部的元素。

+0

这将**不提供鼠标下的元素数组,但实际上只返回最上面的元素。 – 2013-08-21 08:13:15