我希望在WPF Canvas组件上执行矩形碰撞测试,以获取被矩形框架元素覆盖的控件。我发现Silverlight的VisualTreeHelper.FindElementsInHostCoordinates
方法,但显然它在WPF中不可用。
如何实现这样的功能最好呢?
我希望在WPF Canvas组件上执行矩形碰撞测试,以获取被矩形框架元素覆盖的控件。我发现Silverlight的VisualTreeHelper.FindElementsInHostCoordinates
方法,但显然它在WPF中不可用。
如何实现这样的功能最好呢?
var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement);
result
,则如下:var result = new List<DependencyObject>();
//changed from external edits, because VisualHit is
//only a DependencyObject and may not be a UIElement
//this could cause exceptions or may not be compiling at all
//simply filter the result for class UIElement and
//cast it to IEnumerable<UIElement> if you need
//the very exact same result including type
VisualTreeHelper.HitTest(
myUiElement,
null,
new HitTestResultCallback(
(HitTestResult hit)=>{
result.Add(hit.VisualHit);
return HitTestResultBehavior.Continue;
}),
new PointHitTestParameters(myPoint));
GeometryHitTestParameters
而不是PointHitTestParameters
来进行矩形测试。FindElementsInHostCoordinates
有很大的区别,但您应该能够将其用于您的需求。