(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

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

(0 votes)
Denislav Savkov
>
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
0 comments   /   posted on Aug 29, 2008
Tags:   denislav-savkov
Categories:   General , Controls

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!

Subscribe

Comments

No comments

Add Comment

Login to comment:
  *      *       
Login with Facebook