我有一个具有不同列数的 WPF DataGrid。我想根据值对单个单元格进行着色。例如:如果单元格值为 0,则为红色。
这些是我的实验:
<DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DataGrid" SelectionUnit="Cell">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<!--experiment 1 -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
<Setter Property="Background" Value="LimeGreen"/>
</DataTrigger>
<!--experiment 2 -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
<Setter Property="Background" Value="LimeGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>