Is there a way to catch a MouseLeftButtonDown (or other mouse event) at a page or user control level and then be able to determine which child control was actually clicked?
+2
A:
Yes. Set up your handler on the page or other UI root, then use the following method to determine what was clicked within that handler.
List<UIElement> hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(args.GetPosition(null), startFromControl);
Note that there are some requirements for controls to be "hit". One common cause is not having a background defined. Controls may also turn hit testing off with the UIElement.IsHitTestVisible property.
BC
2009-02-17 14:09:30
Thanks, that's just what I need.
Steve Crane
2009-02-17 14:53:37
There is a problem though. Certain controls, buttons for instance, catch the click before the page level handler, which puts a spanner in the works. Looks like I still may need to handle the clicks on those controls. I was hoping to have just the one handler per page.
Steve Crane
2009-02-17 15:15:01
Yes, the click events bubble up the tree. You would have to use a control hierarchy that accommodated that feature.
BC
2009-02-17 15:51:38