我在应用程序中的画布上布置了一系列控件。我正在以编程方式在此画布上生成一个点。我想检测一下画布的子对象是否与此点相交。
我希望可以使用一个命中测试API(wpf曾经有一个可用的接口),但是看起来所有交互似乎都是通过输入和触摸事件进行的,这使我自己难以执行查询。我很可能在搜索中错过了此功能,有人知道如何实现这种功能吗?
您可以使用Windows.UI.Xaml.Media.VisualTreeHelper。使用FindElementsInHostCoordinates查找与特定坐标相交的UIElement。
要找到相对于Canvas的点,您需要将Canvas的坐标转换为应用程序窗口的坐标,您可以使用UIElement进行转换。TransformToVisual。这将考虑缩放和翻译
// myCanvas is the canvas you're hittesting on
// generatedPoint is the Point you're trying to hittest
// page is the app window's root visual
// Get a transform from the Canvas' coordinates to the Page
GeneralTransform gt = myCanvas.TransformToVisual(page);
// Use that to convert the generated Point into the page's coords
Point pagePoint = gt.TransformPoint(generatedPoint);
// and get the elements in the canvas at that point
var elements = VisualTreeHelper.FindElementsInHostCoordinates(pagePoint,myCanvas);
// elements contains the UIElements at generatedPoint (including myCanvas)
该FindElementsInHostCoordinates文档穿行命中更详细的测试方案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句