in WPF when using VisualTreeHelper.HitTest
even hidden Elements are found. To skip this elements and only return a result for visible ones, i created a HitTestFilter
like the following :
///This Filter should cut all invisible or not HitTest Enabled Elements
private static HitTestFilterBehavior MyHitTestFilter(DependencyObject target)
{
var uiElement = target as UIElement;
if (uiElement != null){
if(!uiElement.IsHitTestVisible || !uiElement.IsVisible))
return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
}
return HitTestFilterBehavior.Continue;
}
This Filter does his job, but i like to know what the default WPF HitTesting does in this case? Does it use a similar filter? Are there any other, maybe better options for doing this?
To clarify a short description :
In the image there is
If i got such a layout and do a mouseclick in the green area of Button2, WPF skips Button2 and the click event appears on Button1.
If i do manual HitTesting without the filter described earlier, i will get Button2 as the result.
So the question is, what is the default behaviour/filter WPF is using?
Thanks in advance for any suggestions.
I know this is rather old question, but I had similar issue recently and got it working by using UIElement.InputHitTest method.
I replaced
HitTestResult hitTest = VisualTreeHelper.HitTest(rootVisual, point);
IInputElement source = hitTest?.VisualHit as IInputElement;
With
IInputElement source = rootVisual.InputHitTest(point);
The second version skips invisible elements (while the first doesn't).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With