WinUI3:如何修改RichTextBlock内HyperLink的PointerOver状态画笔
解决WinUI3 RichTextBlock中HyperLink悬停前景色无法自定义的问题
问题原因
HyperLink的默认样式中,PointerOver状态的前景色绑定了系统主题资源HyperlinkForegroundPointerOver,直接在RichTextBlock的Resources中覆盖该资源无法生效,是因为资源查找优先级规则:系统主题资源会优先于父容器的自定义资源被加载。
解决方案1:在HyperLink自身Resources中覆盖主题资源
将自定义的HyperlinkForegroundPointerOver画笔放在HyperLink的Resources中,确保HyperLink在查找资源时优先使用自定义值:
<Grid Margin="20" Width="200"> <RichTextBlock x:Name="SelectCustomerWrapPanel" VerticalAlignment="Top" Grid.Column="1" FocusVisualSecondaryBrush="{ThemeResource SystemAccentColor}" FocusVisualPrimaryBrush="{ThemeResource SystemAccentColor}" TextWrapping="Wrap" LineHeight="20" Margin="0,0,16,0" HorizontalAlignment="Left" Foreground="Orange"> <Paragraph> <Run Text="Attention!" Foreground="Red"/> <Run Text="You are in a wrapped content for checking the pointerover behavior" Foreground="Lavender"/> <Hyperlink Foreground="Orange" UnderlineStyle="Single" x:Name="ActionBanner" IsTabStop="True"> <!-- 覆盖HyperLink悬停状态的前景色资源 --> <Hyperlink.Resources> <SolidColorBrush x:Key="HyperlinkForegroundPointerOver" Color="Black"/> </Hyperlink.Resources> <Run Text="Hover this then it should change to black but it will set the accent color"/> </Hyperlink> </Paragraph> </RichTextBlock> </Grid>
解决方案2:重写HyperLink的ControlTemplate
完全自定义HyperLink的样式,直接指定PointerOver状态的前景色,这种方式更灵活,可同时修改其他交互状态的样式:
<Grid Margin="20" Width="200"> <RichTextBlock x:Name="SelectCustomerWrapPanel" VerticalAlignment="Top" Grid.Column="1" FocusVisualSecondaryBrush="{ThemeResource SystemAccentColor}" FocusVisualPrimaryBrush="{ThemeResource SystemAccentColor}" TextWrapping="Wrap" LineHeight="20" Margin="0,0,16,0" HorizontalAlignment="Left" Foreground="Orange"> <RichTextBlock.Resources> <!-- 自定义HyperLink样式 --> <Style TargetType="Hyperlink"> <Setter Property="Foreground" Value="Orange"/> <Setter Property="UnderlineStyle" Value="Single"/> <Setter Property="IsTabStop" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Hyperlink"> <TextBlock x:Name="TextElement" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" UnderlineStyle="{TemplateBinding UnderlineStyle}"/> <ControlTemplate.Triggers> <!-- 鼠标悬停状态 --> <Trigger Property="IsPointerOver" Value="True"> <Setter TargetName="TextElement" Property="Foreground" Value="Black"/> </Trigger> <!-- 点击按压状态(可选) --> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="TextElement" Property="Foreground" Value="DarkGray"/> </Trigger> <!-- 获得焦点状态(可选) --> <Trigger Property="IsFocused" Value="True"> <Setter TargetName="TextElement" Property="Foreground" Value="Black"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </RichTextBlock.Resources> <Paragraph> <Run Text="Attention!" Foreground="Red"/> <Run Text="You are in a wrapped content for checking the pointerover behavior" Foreground="Lavender"/> <Hyperlink x:Name="ActionBanner"> <Run Text="Hover this then it should change to black but it will set the accent color"/> </Hyperlink> </Paragraph> </RichTextBlock> </Grid>
内容的提问来源于stack exchange,提问作者Akilan
相关产品推荐
相关产品推荐

