2008-09-05 93 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