WPF HitTest default behavior with hidden / folded elements

in WPF when using VisualTreeHelper.HitTest even hidden elements are found. To skip these elements and return the result only for visible ones, I created HitTestFilter as follows:

 ///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 its job, but I like to know what WPF HitTesting does by default in this case? Does he use a similar filter? Are there other possibly better options for this?

To clarify the short description:

Layout example

The image has

  • Layout container as root element
  • Button 1, which is visible
  • Above Button1 is Button2, which is invisible

If I get such a layout and make a mouseclick in the green zone of Button2, WPF skips Button2 and the click event appears on Button1.

If I do a manual HitTesting without the filter described earlier, I get Button2 as an example.

So the question is, what is the default WPF behavior / filter?

Thanks in advance for any suggestions.

+7
c # wpf hittest
source share
1 answer

I know this is a pretty old question, but lately I had a similar problem and started working using the UIElement.InputHitTest method.

I replaced

 HitTestResult hitTest = VisualTreeHelper.HitTest(rootVisual, point); IInputElement source = hitTest?.VisualHit as IInputElement; 

FROM

 IInputElement source = rootVisual.InputHitTest(point); 

The second version skips invisible elements (while the first does not work).

+3
source share

All Articles