Wpf-将颜色应用于数据网格中的特定单元格

时间:2017-09-05 12:32:07

标签: wpf xaml

我正在尝试将颜色应用于数据网格的特定单元格。我做了一些研究,但最终改变了整行的颜色,而不是我想要的。

我的要求就是这个

enter image description here

2 个答案:

答案 0 :(得分:1)

为特定列定义CellStyle

<DataGridTextColumn Header="..." Binding="{Binding Name}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

您可以使用DataTriggers有条件地应用前景:

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Name}" Value="30">
            <Setter Property="Foreground" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

由于XAML中没有定义<>运算符,因此如果值小于或大于,则必须使用转换器来设置Foreground一些价值。

答案 1 :(得分:0)

如果你不想要感兴趣的xaml,这段代码可能适用于你的需要。 (xx,yy是行和冒号)

DataGridCell cell =(YourDgName.Columns [XX] .GetCellContent(DgCagrilar.Items [YY]))。Parent s DataGridCell; if(cell.Background == Brushes.Pink)cell.Background = Brushes.Plum; else cell.Background = Brushes.Pink;

相关问题