(0 votes)

Tip: How to HitTest with coordinates relative to an element (instead of relative to the application root)?

0 comments   /   posted by Denislav Savkov on Aug 29, 2008

In Silverlight for hit testing are used the absolute coordinates of the application. In case you want to hit test with coordinates relative to a System.Windows.UIElement you need to get the coordinates of the UIElement relative to the application root (i.e. the absolute coordinates of an element).

C#

GeneralTransform generalTransform = uiElement.TransformToVisual( Application.Current.RootVisual);
Point elementToApplicationRootCoords = generalTransform.Transform( new Point( 0, 0 ) );

And then add them to the coordinates relative to the element.

C#

Rect areaInAbsoluteCoordinates =
    new Rect( areaRelativeLeftCoordinate + elementToApplicationRootCoords.X,
              areaRelativeTopCoordinate + elementToApplicationRootCoords.Y,
              areaWidth, areaHeight );
IEnumerable<UIElement> childrenInArea = uiElement.HitTest(areaInAbsoluteCoordinates );
 
That's it!
Filed under: Controls and UI


Comments

Comments RSS RSS
No comments

Add Comment