I am trying to change the style for selected cells the WPF DataGrid control.
What is the difference between styling DataGridCell and DataGridRow? I tried various combinations below and they all work. However, it seems like I only need to style either DataGridCell or DataGridRow.
What is the purpose of having both? This tells me I am misunderstanding their purpose.
Style for DataGridCell: (in my own code, I change the colors from the defaults)
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
Style for DataGridRow: (in my own code, I change the colors from the defaults)
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
DataGridRow will be applied to the whole Row. If you want to set any property of the whole row, you can use DataGridRow.
Each DataGridRow contains a list of DataGridCell. You can define a style for that too. If you dont, it will take up the style from the DataGridRow.
Generally, we specify the latter to define the differentiation between two adjacent cells, like specifying a margin between cells, borders etc.