TextBlock inlines and hit testing
Posted: 15 Feb 2025, 23:17
It appears VisualTreeHelper::HitTest may return TextElement inlines from a TextBlock. I can see how this may be an useful feature, but this isn't the case in WPF.
This behavior leads to a couple of issues.
First, I can't simply traverse the visual tree upwards via VisualTreeHelper::GetParent, because it returns null for a TextElement. I noticed it long ago, but it didn't bother me much because it is easy to work around.
However the second one is worse: IsMouseOver triggers still work on a disabled element if the mouse happens to be over a text contained in an inline.
It can be easily reproduced in XamlToy:
The block changes color to blue in Noesis when I move the mouse over text, but doesn't do so in WPF.
Is it a bug or by design? Do you have any suggestions on how to avoid this?
This behavior leads to a couple of issues.
First, I can't simply traverse the visual tree upwards via VisualTreeHelper::GetParent, because it returns null for a TextElement. I noticed it long ago, but it didn't bother me much because it is easy to work around.
However the second one is worse: IsMouseOver triggers still work on a disabled element if the mouse happens to be over a text contained in an inline.
It can be easily reproduced in XamlToy:
Code: Select all
<TextBlock Width="400" Height="200" IsEnabled="False">
<Span Foreground="White" FontSize="50">Mouse over text</Span>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Is it a bug or by design? Do you have any suggestions on how to avoid this?