Reputation: 21299
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>
Upvotes: 2
Views: 11102
Reputation: 2992
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.
Upvotes: 4
Reputation: 618
as @abhishek said ... I have a situation that I need to use both row style and cell style that is my row style
<!-- Row Style-->
<Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
then I need specific cells to be white background while selected I used
<Style x:Key="TimeCell" TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
and give the specific columns the style
<dg:DataGridTemplateColumn Width="120" CellStyle="{StaticResource TimeCell}">
I hope that is clear what I want to say
Upvotes: 2