I have a WPF ParentUserControl and some ChildUserControlA ChildUserControlB etc. (also WPF controls)
The ChildUserControls are controls containing on them some other basic elements (textboxes, labels, etc)
In the main form’s status bar I need to display the name of the currenlty mouse overed ChildUserControl.
So, in the ParentUserControl‘s MouseMove I obtain an object myElement = Mouse.DirectlyOver because I need the top most of ChildUserControl‘s, but I obtain instead a ‘System.Windows.Controls.TextBlock’ (part of ChildUserControl)…
How to fix that?
I need when I mouseOver an MyChildControl that the MyChildControl be detected, and not its child elements.
In this order I used on MyChildControl the following:
Protected Overrides Function HitTestCore(
hitTestParameters As PointHitTestParameters) As HitTestResult
Return New PointHitTestResult(Me, hitTestParameters.HitPoint)
End Function
but anyway I get sometimes the textblock as result, sometimes the ChildUserControl…
Another answer may be something like this – define the exact types of objects you care about and work up the visual tree until you find them :