How to make a WPF DataGrid with alternating row colors and a patch color section (ignoring striping)

I have a datagrid which I am trying to make similar: enter image description here

I use the AlternatingRowBackground attribute to execute alternating colors. For a fixed color section, I have XAML that resembles:

  <DataGrid.Resources> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=ShouldBeFixedColor}" Value="True"> <DataTrigger.Setters> <Setter Property="Background" Value="Blue" /> </DataTrigger.Setters> </DataTrigger> </Style.Triggers> </Style> </DataGrid.Resources> 

The problem with this approach is that the "alternating color" takes precedence over a fixed color trigger. So below, instead of blue-blue, blue-gray-blue.

Any ideas on how to archive the desired color? I would prefer to do all this at the XAML level, if possible.

Thanks!

+6
source share
2 answers

Made some changes based on other SO answers. Hope this helps someone in the future.

  • Yankees AlternatingRowBackground=... from the grid. Add AlternationCount="2"
  • Add the block below to make the style (manually make alternating lines)

      <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="AlternationIndex" Value="0"> <Setter Property="Background" Value="White" /> </Trigger> <Trigger Property="AlternationIndex" Value="1"> <Setter Property="Background" Value="WhiteSmoke" /> </Trigger> <DataTrigger Binding="{Binding Path=Selectable}" Value="False"> <DataTrigger.Setters> <Setter Property="Background" Value="LightGray" /> </DataTrigger.Setters> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> 
+29
source

If someone else is looking for the same in code:

 Style rowStyle = new Style(typeof(DataGridRow)); Trigger rowTrigger = new Trigger(); rowTrigger.Property = DataGridRow.AlternationIndexProperty; rowTrigger.Value = 0; Setter rowSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Yellow); rowTrigger.Setters.Add(rowSetter); Trigger alternateRowTrigger = new Trigger(); alternateRowTrigger.Property = DataGridRow.AlternationIndexProperty; alternateRowTrigger.Value = 1; Setter alternateRowSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Pink); alternateRowTrigger.Setters.Add(alternateRowSetter); DataTrigger rowDataTrigger = new DataTrigger(); rowDataTrigger.Value = true; rowDataTrigger.Binding = new Binding() { Path = new PropertyPath(nameof(MyObject.IsSomethingTrue))}; Setter backgroundSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Blue); Setter foregroundSetter = new Setter(DataGridRow.ForegroundProperty, Brushes.White); rowDataTrigger.Setters.Add(backgroundSetter); rowDataTrigger.Setters.Add(foregroundSetter); // the order of the triggers may not be changed as explained by CptCoathanger rowStyle.Triggers.Add(rowTrigger); rowStyle.Triggers.Add(alternateRowTrigger); rowStyle.Triggers.Add(rowDataTrigger); RootDataGridOrders.RowStyle = rowStyle; 
0
source

Source: https://habr.com/ru/post/920231/


All Articles