Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a WPF DataGrid with alternating row colors, and a fix color section (ignoring alternation)

I have a datagrid that I'm trying to make resemble: enter image description here

I'm using the AlternatingRowBackground attribute to perform the alternating colors. For the 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 the fixed color style trigger. So, at the bottom instead of blue-blue-blue it is blue-gray-blue.

Any ideas on how to archive the desired coloring? I'd rather do this all at the XAML level if possible.

Thanks!

like image 306
jglouie Avatar asked Jul 11 '12 14:07

jglouie


1 Answers

Made some changes based upon other SO answers. Hopefully this helps someone in the future.

  1. Yank AlternatingRowBackground=... from the grid. Add AlternationCount="2"
  2. Add the block below to do the styling (manually doing the alternating rows)

            <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>
    
like image 172
jglouie Avatar answered Nov 09 '22 03:11

jglouie