WPF中与Silverlight的FindElementsInHostCoordinates相当的是什么?

7

我希望在WPF Canvas组件上执行矩形碰撞测试,以获取被矩形框架元素覆盖的控件。我发现Silverlight的VisualTreeHelper.FindElementsInHostCoordinates方法,但显然它在WPF中不可用。

如何实现这样的功能最好呢?

2个回答

3
假设你在Silverlight中有这样一个调用:
var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement);

如果这段WPF代码应该有一个等效的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来进行矩形测试。

3
最接近的等价物是VisualTreeHelper.HitTest。它与Silverlight的FindElementsInHostCoordinates有很大的区别,但您应该能够将其用于您的需求。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接