What is Silverlight FindElementsInHostCoordinates equivalent in WPF?

I would like to do a rectangular hit test on a WPF Canvas component to get controls that overlap with a Rectangle framework element. I found the Silverlight VisualTreeHelper.FindElementsInHostCoordinates method, but apparently it is not available in WPF.

What is the best way to achieve this functionality?

+7
c # wpf silverlight hittest
source share
2 answers

The closest equivalent is VisualTreeHelper.HitTest . It is significantly different from Silverlight FindElementsInHostCoordinates , but you can use it for your needs.

+3
source share

Suppose you have such a call in Silverlight

 var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement); 

then this WPF code should have the equivalent of 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)); 

in your special case, you can use GeometryHitTestParameters instead of PointHitTestParameters to perform a Rect-Test.

+3
source share

All Articles