Mathak Mathak - 3 months ago 22
C# Question

Change background for specific rows in datagrid wpf

I have a datagrid "Funds" bind to a listcollectionview.

I want to highlight specific rows in datagrid based on few rules. The fundGroupsList is the list of group names. The rule1 is to find the misalignment within members of a group.

Answer

One of the ways to do that is to create a boolean property (ex. IsRule1Failed) in Entries and save the fail result foreach row.

Then, in XAML use DataTriggers to change the background color.

<DataGrid.Resources>
      <Style TargetType="DataGridCell">
          <Style.Triggers>
             <Trigger Property="IsSelected" Value="True">
                 <Setter Property="Background" Value="BurlyWood" />
             </Trigger>
             <DataTrigger Binding="{Binding DataContext.IsRule1Failed}" Value="True">
                 <Setter Property="Background" Value="Red"></Setter>
             </DataTrigger>
          </Style.Triggers>
      </Style>
 </DataGrid.Resources>