Reputation: 1712
I want to change the row color of a DataGrid based on a boolean value. I've looked at https://stackoverflow.com/questions/18580612/wpf-datagrid-trigger-row-colour-based-on-value and tried it in a similar manner (defining the style locally instead of defining it in the usercontrol's ressources), but the row background won't change when the editable
property changes. I don't know what's wrong and would be glad to have some new things to try.
<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
RowBackground="Wheat" AlternatingRowBackground="WhiteSmoke" VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">
<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="LightBlue" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 3
Views: 4281
Reputation: 4322
If you take out the RowBackground and AlternatingRowBackground from the DataGrid, it works. You will have to put those into a style for the DataGrid if you want to use them.
<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="RowBackground" Value="Wheat" />
<Setter Property="AlternatingRowBackground" Value="WhiteSmoke" />
</Style>
</DataGrid.Style>
<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable,UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 5