Recommended

Skip Navigation LinksHome / Tips / View Tip

Tip: How to find out the UI elements located in an area?

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

In some scenarios we need to find out what are the objects located at a specific point or in a certain area. In this case we get access to the System.Windows.UIElelement objects using the UIElement.HitTest method. It can test a Rect area or a Point. The return value is a collection of the children in this area arranged from the top element to the bottom.

C#

Rect areaInAbsoluteCoordinates = new Rect( areaLeftCoordinate, areaTopCoordinate, areaWidth, areaHeight );
IEnumerable<UIElement> childrenInArea = panel.HitTest(areaInAbsoluteCoordinates );

Note! In Silverlight the coordinates for hit testing are the absolute coordinates of the application. In case you want to hit test with coordinates relative to an UIElement see our tip.

Note! To get all the elements in an area you must use Application.Current.VisualRoot.HitTest.

That’s it!

Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 2 and 4 and type the answer here:

Join the free SilverlightShow webinar 'Silverlight's Power Features: Data Binding in Action' on August 19th 2010, 10:00 am PDT.
In this session Gill Cleeren will make with a complete overview of the data binding features Silverlight 4 has to offer. He will discuss how data binding is the enabler for applying the MVVM pattern and commanding, and will end up reviewing Blend's support for data binding, including the use of design-time data. Read more | Register Now (hide this)