You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataGridRow选中时无法更改Hyperlink前景色问题求助

DataGrid选中行时Hyperlink前景色不切换的解决方法

问题场景

我正在修改一个绑定搜索结果到DataGrid的程序,要求选中DataGrid行时,列内所有文本前景色变为白色,取消选中时恢复黑色。但添加了包含Hyperlink的静态列后,Hyperlink无法在选中行时切换为白色,相关代码如下:

<Window.Resources>
    <Style TargetType="TextBlock"
           x:Key="TextBlockStyle">
        <Setter Property="HorizontalAlignment"
                Value="Left" />
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="Foreground"
                Value="{StaticResource PopupForegroundBrush}" />
        <Setter Property="FontSize"
                Value="18" />
        <Setter Property="TextWrapping"
                Value="Wrap" />
        <Setter Property="MaxWidth"
                Value="125" />
    </Style>
    <Style TargetType="Hyperlink" x:Key="DataGridHyperlinkStyle">
        <Setter Property="Foreground" Value="Black"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
                <Setter Property="Foreground" Value="White"/>
                </DataTrigger>
            </Style.Triggers>
    </Style>
    <converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

<DataGridTemplateColumn Header="Other Location QOH"
                        Width="1*"
                        IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Padding="5">
                <Hyperlink Click="otherQohHyperlink_Click" TextDecorations="None" Tag="{Binding}" Style="{StaticResource DataGridHyperlinkStyle}"> 
                    Other Location QOH Search 
                 </Hyperlink>   
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

解决方法

方法一:修复Hyperlink样式的绑定逻辑

原样式中RelativeSource的绑定未指定查找模式,可能导致无法正确定位到DataGridRow。修改Hyperlink样式,明确绑定路径:

<Style TargetType="Hyperlink" x:Key="DataGridHyperlinkStyle">
    <Setter Property="Foreground" Value="Black"/>
    <Style.Triggers>
        <!-- 明确指定查找父级DataGridRow的模式 -->
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Mode=FindAncestor}}" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </DataTrigger>
        <!-- 可选:添加鼠标悬停样式优化交互 -->
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="#FF0066CC"/>
        </Trigger>
    </Style.Triggers>
</Style>

方法二:全局设置DataGridRow的选中样式

如果希望行内所有文本(包括TextBlock和Hyperlink)统一变色,可直接给DataGridRow添加样式,让子元素继承前景色:

<Window.Resources>
    <!-- 保留原有资源 -->
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

这种方式无需单独修改Hyperlink样式,所有行内文本元素都会继承Row的Foreground属性,更简洁。

注意事项

  • 若TextBlock设置了固定Foreground,Hyperlink默认会继承该值,但只要Hyperlink自身样式中设置了Foreground(包括触发状态的值),就会覆盖继承属性。
  • 确保DataGrid的SelectionUnit属性为FullRow(默认值),点击行任意位置都能触发选中状态。

内容的提问来源于stack exchange,提问作者Robert Zantarra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:37:43