wpf datagrid alternate row coloring

WpfXamlWpf ControlsStylesWpfdatagrid

Wpf Problem Overview


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>

Wpf Solutions


Solution 1 - Wpf

Finally, this is what I ended up with for generically setting alternate row colors.

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Background" Value="#FFF" />
    <Setter Property="AlternationCount" Value="2" />
</Style>

 <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#CCC"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#EEE"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Solution 2 - Wpf

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.

Solution 3 - Wpf

Try setting the alternating background like this:

  AlternationCount="2" AlternatingRowBackground="Bisque"

Solution 4 - Wpf

Try this

  <DataGrid AlternationCount="2"
            AlternatingRowBackground="Salmon" ........

Solution 5 - Wpf

Finally I used combination of Robin Maben and Th3G33k solution, because I want the alternation colour to override with my own, when some condition is met. Thanks both.

<DataGrid x:Name="gridCustomerOrderItems" HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch" AutoGenerateColumns="False"
                  AlternationCount="2"
                  IsReadOnly="True" CanUserReorderColumns="True"
                      ScrollViewer.CanContentScroll="True"
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Auto">

                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Style.Triggers>
                            <!--first alteraniting colour-->
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter Property="Background" Value="#EEE"></Setter>
                            </Trigger>
                            <!--then override with my own colour-->
                            <DataTrigger Binding="{Binding InvoiceSet}" Value="True">
                                <Setter Property="Background" Value="Green"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin MabenView Question on Stackoverflow
Solution 1 - WpfRobin MabenView Answer on Stackoverflow
Solution 2 - Wpfmatthias.lukaszekView Answer on Stackoverflow
Solution 3 - WpfTh3G33kView Answer on Stackoverflow
Solution 4 - WpfAlan392View Answer on Stackoverflow
Solution 5 - WpfrostaView Answer on Stackoverflow