有条件地设置DataGrid RowStyle突出显示背景和文本

时间:2015-01-07 19:08:29

标签: c# wpf wpfdatagrid wpf-style

我有一个DataGrid,我根据绑定属性QualityStatus突出显示行颜色/文本。它工作正常,但默认的行突出显示会破坏行颜色。我意识到我可以将HighlightBrushKey设置为Transparent,因此颜色不会改变,但这不会影响“非活动”颜色,就像你选择了一行然后聚焦另一个控件一样。此外,我不知道如何设置选定的行字体颜色。

理想情况下,我只有标签,我可以为3个条件中的每个条件设置高亮背景/文字颜色,但我不确定如何做到这一点。

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderBrushKey}" Color="Transparent" />
</DataGrid.Resources>
<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding QualityStatus}" Value="Poor">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontWeight" Value="Bold"/>

            </DataTrigger>
            <DataTrigger Binding="{Binding QualityStatus}" Value="Fair">
                <Setter Property="Background" Value="Yellow"/>
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding QualityStatus}" Value="Good">
                <Setter Property="Background" Value="LightGreen"/>
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="FontWeight" Value="Normal"/>
            </DataTrigger>

        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

1 个答案:

答案 0 :(得分:0)

适用于.Net 4.0:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />

这就是你改变文字颜色的方法:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="White"/>
相关问题