Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 278
Robin Maben Avatar asked Jan 25 '11 10:01

Robin Maben


4 Answers

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>
like image 191
Robin Maben Avatar answered Nov 18 '22 17:11

Robin Maben


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.

like image 44
matthias.lukaszek Avatar answered Nov 18 '22 18:11

matthias.lukaszek


Try setting the alternating background like this:

  AlternationCount="2" AlternatingRowBackground="Bisque"
like image 33
Th3G33k Avatar answered Nov 18 '22 19:11

Th3G33k


Try this

  <DataGrid AlternationCount="2"
            AlternatingRowBackground="Salmon" ........
like image 3
Alan392 Avatar answered Nov 18 '22 18:11

Alan392