i've a WPF DataGrid with different count of columns. I want to color the single cells dependent by the value. For example: If the cell-value is 0, then red.
These are my experiments:
<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>
Just use a value converter (with the cell value as a parameter), that returns the color you want.
EDIT: Finally got it to work.
Converter and style definitions:
The DataGrid:
And the converter:
I used the
DataGrid_Loaded
method to fill the DataGrid with some random data encapsulated in a sample class:And the result:
Use a value converter, like this:
And: