Search code examples
wpfxamlwpf-controlsstyleswpfdatagrid

wpf datagrid alternate row coloring


I have tried this method.. without luck..

 <Style TargetType="{x:Type DataGridRow}">
  <Style.Triggers>
      <Trigger Property="ItemsControl.AlternationIndex" Value="0">
          <Setter Property="Foreground" Value="Red" />
     </Trigger>
  </Style.Triggers>
</Style>

Is there a way to get the row Index? I have even tried

<DataTrigger Binding="{Binding AlternationIndex}" Value="0">
    <Setter Property="Foreground" Value="Green"></Setter>
</DataTrigger>

Solution

  • Unless already done, you have to set the AlternationCount property of DataGrid:

    <DataGrid AlternationCount="2"
              ... />
    

    You should additionally check whether the Foreground property is used for any Control in the DataGridRow. Try setting the Background property to test the alternation stuff.