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

如何仅为Calculation Functions标签页的DataGrid行设置红前景与背景样式

问题原因

你原有代码不生效的核心问题是:DataGridRow 本身不存在 Header 属性,你写的触发器绑定的对象错误,自然不会触发样式修改。

实现方案

只需要把普通Trigger换成DataTrigger,通过相对路径查找当前DataGrid所属的父级TabItem,匹配其Header值即可,纯XAML修改无需调整后台逻辑,修改后的代码如下:

<DataGrid Name="ReflectionDataGrid" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding List}" AutoGenerateColumns="False" IsHitTestVisible="True">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}" Value="Calculation Functions">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle> 
</DataGrid>

注意事项

如果你的TabItem.Header不是直接赋值的字符串,而是嵌套了TextBlock等控件,只需要调整DataTrigger的绑定路径,匹配Header内控件的对应文本属性即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 07:54:03