Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top