Reputation: 3912
Is it possible within Silverlight 4, to find out which control is being clicked on? There is a property on all UIElements which is called IsHitTestVisible, but i cant actual perform a hittest on the controls?
Upvotes: 1
Views: 1745
Reputation: 189439
You can discover mouse clicks via the MouseLeftButtonDown
or the MouseLeftButtonUp
events (or the equivalent RightButton events).
These are bubbling events which means if they aren't handled but the actual element clicked the event will fire on the containing element and so on up to the root item.
Hence for many simple elements you can add a handler to the containing element and the use OriginalSource
property of the MouseButtonEventArgs
to determine which element actually generated the message.
However you use the word "Control" which some use loosely to mean UI elements others use more strictly to refer to things like a ListBox
. In the latter case these controls may handle the mouse events so they don't bubble any further.
You can still get the LeftButton events using AddHandler
on the containing UIElement even the ones that have been handled. That would still leave you with the problem of discovering the actual control you are interested in since the OriginalSource
will be some component of the control and hence a descendent of the control.
Upvotes: 1
Reputation: 70112
You can hit test with VisualTreeHelper.FindElementsInHostCoordinates as described in MSDN docs:
http://msdn.microsoft.com/en-us/library/cc838402%28v=VS.95%29.aspx
However, a simpler approach might be to add handlers to the MouseLeftButtonUp events on the specific elements you are interested in hit testing.
Upvotes: 2