HitTest doesn't work for IsHitTestVisible?
The UserControl contains a Grid, which in turn contains a TextBlock. Both UserControl and Grid don't have a Transparent background. TextBlock has the IsHitTestVisible=False property. But VisualTreeHelper.hitTest returns a TextBlock. Can I find out how to properly configure ignoring specific UI elements?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: HitTest doesn't work for IsHitTestVisible?
It may seem strange but the HitTest method without callbacks returns hits for all visuals, even if they have IsHitTestVisible=False (also those with Visibility=Hidden, or Opacity=0, or IsEnabled=False).
But you can use the version with callbacks to supply your own Filter and Result callbacks:
But you can use the version with callbacks to supply your own Filter and Result callbacks:
Code: Select all
public static class VisualTreeHelper {
public static void HitTest(Visual reference,
HitTestFilterCallback filterCallback,
HitTestResultCallback resultCallback,
HitTestParameters hitTestParameters);
}
Re: HitTest doesn't work for IsHitTestVisible?
Had seen. Thanks. Is there a detailed example of using callbacks in the documentation in C#? For example, what are the HitTestFilterBehavior and HitTestResultBehavior enums responsible for?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: HitTest doesn't work for IsHitTestVisible?
HitTestFilterBehavior
- ContinueSkipSelfAndChildren: Do not hit test against the current Visual or its descendants
- ContinueSkipChildren: Hit test against the current Visual, but not its descendants
- ContinueSkipSelf: Do not hit test against the current Visual, but hit test against its descendants
- Continue: Hit test against the current Visual and its descendants
- Stop: Stop hit testing at the current Visual
- Stop: Stop any further hit testing and return from the callback
- Continue: Continue hit testing against the next visual in the visual tree hierarchy
Code: Select all
DependencyObject hit = null;
bool HitTest(Visual root, Point p)
{
VisualTreeHelper.HitTest(root, OnFilter, OnHit, new PointHitTestParameters(p);
return hit != null;
}
HitTestFilterBehavior OnFilter(Visual target)
{
UIElement element = target as UIElement;
if (target != null)
{
if (!element.IsHitTestVisible || !element.IsVisible)
{
return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
}
}
return HitTestFilterBehavior.Continue;
}
HitTestResultBehavior OnHit(HitTestResult result)
{
hit = result.VisualHit as Visual;
if (hit != null)
{
return HitTestResultBehavior.Stop;
}
return HitTestResultBehavior.Continue;
}
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 71 guests