根据单元格内容设置DataGrid行的背景

6

使用XAML,是否有一种方法可以根据其单元格内容动态设置行的背景?

谢谢,

Phil

1个回答

18

您可以使用DataTrigger为行定义样式并更改颜色,例如:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding BooleanPropertyOnObjectBoundToRow}" Value="True">
                   <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

这里的BooleanPropertyOnObjectBoundToRow是您的数据对象上的一个布尔属性,其中一个单元格绑定到该属性。


如何使颜色动态化而不仅仅是红色? - Phil Gan
抱歉,也许我误解了。我的属性实际上是一个“枚举”,我想我可以为绑定制作一个枚举到布尔值的转换器。 - Phil Gan
6
@Phil: 你也可以为Binding的Value属性指定自己的枚举值。为此,你必须声明枚举的命名空间,并在value属性中设置它,格式为Value="{x:Static yourNamespace:YourEnum.YourValue}"。 - HCL
谢谢,我自己永远也想不出来。 - Phil Gan
我不确定我理解了这个:Binding="{Binding BooleanPropertyOnObjectBoundToRow}" 这是什么? - RSM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接