什么是WPF中Silverlight的FindElementsInHostCoordinates等价物?

时间:2010-01-13 19:11:06

标签: c# wpf silverlight hittest visualtreehelper

我想在WPF Canvas组件上执行rectangual命中测试,以获得与Rectangle框架元素重叠的控件。我找到了一个Silverlight的VisualTreeHelper.FindElementsInHostCoordinates方法,但显然它在WPF中不可用。

实现此类功能的最佳方法是什么?

2 个答案:

答案 0 :(得分:3)

最接近的等价物是VisualTreeHelper.HitTest。它与Silverlight FindElementsInHostCoordinates的工作方式大不相同,但您应该能够根据自己的需要使用它。

答案 1 :(得分: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进行矩形测试。

相关问题