Hit testing
Hit testing determines which visual element is located at a given point on the screen. Avalonia uses hit testing internally for pointer events, but you can also perform hit testing programmatically for custom controls and advanced interaction scenarios.
How hit testing works
When a pointer event occurs, Avalonia walks the visual tree from the topmost element downward. For each element, it checks whether the point falls within the element's bounds and its rendered content. The first element that passes the test becomes the event target.
The hit test considers:
- Visibility: Elements with
IsVisible="False"are skipped. - IsHitTestVisible: Elements with
IsHitTestVisible="False"are skipped, but their children may still be tested. - Bounds: The point must fall within the element's layout bounds.
- Rendered content: For shapes and custom-rendered controls, the test checks actual rendered pixels, not just the bounding box.
IsHitTestVisible
Set IsHitTestVisible="False" to make a control "transparent" to pointer events. The control still renders normally, but pointer events pass through to the control behind it:
<!-- This overlay displays text but passes clicks through to controls underneath -->
<Panel>
<Button Content="Click Me" />
<TextBlock Text="Overlay label" IsHitTestVisible="False"
HorizontalAlignment="Right" VerticalAlignment="Top"
Foreground="Gray" Margin="8" />
</Panel>
Common uses:
- Decorative overlays that should not intercept clicks
- Watermark or status text over interactive content
- Animation layers
Background and hit testing
A control without a Background (or with Background set to null) does not participate in hit testing for its empty area. Only the child content receives pointer events.
To make the entire area of a panel respond to pointer events, set Background="Transparent":
<!-- This panel does NOT receive clicks in empty areas -->
<StackPanel PointerPressed="OnPressed">
<TextBlock Text="Only this text is clickable" />
</StackPanel>
<!-- This panel receives clicks anywhere within its bounds -->
<StackPanel PointerPressed="OnPressed" Background="Transparent">
<TextBlock Text="Click anywhere in the panel" />
</StackPanel>
This rule applies at every level, including the Window itself. Setting Background="{x:Null}" with TransparencyLevelHint="Transparent" lets pointer events pass through empty areas to OS windows underneath. Setting Background="Transparent" looks the same but captures all input. See Transparent click-through window for a complete example.