Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips / View Tip

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

+ Add to SilverlightShow Favorites
0 comments   /   posted by Denislav Savkov on Aug 29, 2008
(0 votes)
Categories: Controls and UI

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!
Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 5 and 6 and type the answer here: