1
votes

I have a DataGrid where I show many lines of data. To help visually differentiate the rows I've added a background colour to alternating rows.

But, there are some rows that contains very interesting data that I want to attract the user's attention to, and so I use a Style DataTrigger to highlight those specific rows.

My problem is that the alternating background colour takes precedence - only the odd rows (no background colour) show the highlight.

Note, this is a data-bound DataGrid using the MVVM pattern (no "code-behind").

The (very cutdown) code is as follows:

<DataGrid ItemsSource="{Binding FilteredTraceMessages, Mode=OneWay}" 
            AlternatingRowBackground="AliceBlue"
            .......>

    <DataGrid.Columns>
        ....
    </DataGrid.Columns>

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Severity}" Value="Error">
                    <Setter Property="Background" Value="LightSalmon"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Severity}" Value="Warning">
                    <Setter Property="Background" Value="LemonChiffon"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
1

1 Answers

5
votes

You have to set the Backround on the same precedence level. See Dependency Property Setting Precedence List
Delete AlternatingRowBackground="AliceBlue" from the DataGrid and put AlternationCount="2" there. Then add at the first place a trigger for AlternationIndex.

<DataGrid ItemsSource="{Binding FilteredTraceMessages, Mode=OneWay}" AlternationCount="2"
    .......>

    <DataGrid.Columns>
        ....
    </DataGrid.Columns>

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="AliceBlue"/>
                </Trigger>
                <DataTrigger Binding="{Binding Severity}" Value="Error">
                    <Setter Property="Background" Value="LightSalmon"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Severity}" Value="Warning">
                    <Setter Property="Background" Value="LemonChiffon"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>