I built a CustomDataGrid
usercontrol based on the DataGrid
where I define the DataGridCell
style trigger to alternate the background color of odd vs. even selected cells as follow:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="1" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="LightYellow" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="0" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFFFF0" />
</MultiDataTrigger>
This style is not propagated to the cell where I redefine the template as follow (I want to color the right border because I freeze this column):
<DataGridTemplateColumn Header="Frozen column with right border">
<DataGridTemplateColumn.CellStyle>
<Style BasedOn="{StaticResource {x:Type DataGridCell}}" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Padding="4,0,4,0"
VerticalAlignment="Center"
Text="{Binding Value}"
TextOptions.TextFormattingMode="Display" />
<Border
Grid.Column="1"
Width="2"
Background="{StaticResource PrimarySolidColorBrush}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
but is correctly propagated for a “normal” cell:
<DataGridTemplateColumn Header="Non-frozen column">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding NonFrozenValue}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I’m sure there is better way to highlight the delimitation of the left frozen columns and the right non-frozen ones, but so far…
The issue is that I don’t get the alternation of yellow when I select the rows of CustomDataGrid
usercontrol (it is quite complex because I added filteringaaa). Nevertheless, everything works fine if I put all the code inside the DataGrid
. Should I use TemplateBinding
or something similar to propagate the style trigger ?