I've got a Datagrid with 3 columns.
Now i want to change the highlight of the row conditionally. like: if the value in column 2 is greater than XX change row color to Red.
I've already tried something, but without success:
<DataGrid Name="DataGrid1" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" AlternatingRowBackground="LightGray" ItemsSource="{Binding}" AutoGenerateColumns="False" FontSize="18" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserAddRows="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Steckzyklen}" Value="< 20">
<Setter Property="Background" Value="#FFFF4848"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Steckplatz" Binding="{Binding Name}" Width="200"/>
<DataGridTextColumn Header="Steckzyklen" Binding="{Binding Steckzyklen}" Width="200"/>
<DataGridTextColumn Header="Austauschdatum" Binding="{Binding Austauschdatum}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
In this line: DataTrigger Binding="{Binding Steckzyklen}" Value="< 20" I tried ti do it with the < but without succes.
Someone knows how to make it work? Preferably by only changing something in the XAML.
Thanks in advance!
You can change the Style as follows:
Create a new Converter:
public class IsEqualOrLessThanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int intValue = (int)value;
int compareToValue = Int32.Parse(parameter.ToString() ?? string.Empty);
return intValue <= compareToValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And then:
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Steckzyklen, Converter={StaticResource IsEqualOrLessThanConverter}, ConverterParameter=20}" Value="True">
<Setter Property="Foreground" Value="#FFFF4848"/>
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>