I would like to display the odd vs. even selected rows with different background color (gray) in a WPF DataGrid. I already use the RowBackground="#DDD", AlternatingRowBackground="#EEE" and AlternationCount=2 properties withing DataGrid itself to alternate the color of non-selected rows.
I've already browsed many threads on StackOverflow, but can't find why the Style.Triggers has no effect. I use this attached property to determine whether it is an odd or even row.
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<!-- Changes the row style template to have more advanced "borders" (dotted, dashed, etc.) based on value converters -->
<Setter Property="Template">
<!-- ... -->
</Setter>
<!-- Changes the background color of odd rows (will do the same for even one with a slightly different color) -->
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}"
Value="True"/>
<Condition Binding="{Binding (behaviors:DataGridBehavior.IsOddRow), RelativeSource={RelativeSource Self}}"
Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background"
Value="LightYellow"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<!-- Same principle for even rows -->
<!-- ... -->
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
Same (no) effect if I use only one Condition in the MutliDataTrigger.
Thanks for any insights :-)
You do not have to use a custom behavior for this, you can use the built-in alternation mechanism.
Create a style for
DataGridRowwith triggers for theIsSelectedandAlternationIndexattached property. Note, that we have to set the row background color and the alternating background color here, too, because setting theRowBackgroundandAlternatingRowBackgroundonDataGridwill overwrite the background values defined in the style and the selection highlighting would not work, so remove them fromDataGrid.Since a
DataGridCellis a child of aDataGridRow, the selection colors defined there will occlude the row colors. To avoid creating the same style forDataGridCell, too, we set its background transparent when it is selected.