在流程設計器中用到了命中測試,主要使用了VisualTreeHelper.FindElementsInHostCoordinates 方法。有兩個重載方法,一個是測試點的命中,一個是測試區域的命中。
檢索一組對象,這些對象位於某一對象的座標空間的指定點內。
參數
intersectingPoint
類型:System.Windows.Point 。要用作確定點的點。
subtree
類型:System.Windows.UIElement 。要在其內進行搜索的對象。
返回值
類型:System.Collections.Generic.IEnumerable 。一組可枚舉的 UIElement 對象,這些對象被確定位於指定點處和指定子樹內的可視化樹組合中。
檢索一組對象,這些對象位於某一對象的座標空間的指定 Rect 內。
參數
intersectingRect
類型:System.Windows.Rect 。 要用作確定區域的 Rect。
subtree
類型:System.Windows.UIElement 。要在其內進行搜索的對象。
返回值
類型:System.Collections.Generic.IEnumerable 。一組可枚舉的 UIElement 對象,這些對象被確定位於指定點處和指定子樹內的可視化樹組合中。
遇到的問題及解決方法
當Canvas的父元素是ScrollViewer時調用FindElementsInHostCoordinates函數時,發現命中的元素不正確,需要使用 Application.RootVisual 作爲 subtree 輸入。
原代碼
修改後代碼