在流程设计器中用到了命中测试,主要使用了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 输入。
原代码
修改后代码