WPF DataGrid RowStyle for the selected row that does not change the background and foreground color

I am using Visual Studio 2012 for Windows 7. I need to know why the following style for the selected Grid line does not work for background and foreground colors, but works fine for other properties like BorderBrush and BorderThickness etc.? Although I see how they change when the mouse is over grids.

<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="PeachPuff"/> <Setter Property="Foreground" Value="BlueViolet"/> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="PeachPuff"/> <Setter Property="Foreground" Value="BlueViolet"/> <Setter Property="BorderBrush" Value="BlueViolet" /> <Setter Property="BorderThickness" Value="2" /> </Trigger> </Style.Triggers> </Style> 

This is how I use the grid.

 <DataGrid RowStyle="{StaticResource gridRowStyle}"> 

I emphasize that I know the “why” and not the solution to the problem, since I already have a solution to the problem if I use the grid cell style instead of the row, for example:

 <Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="PeachPuff"/> <Setter Property="Foreground" Value="BlueViolet"/> </Trigger> </Style.Triggers> </Style> 
+6
source share
2 answers

In the Default DataGridCell style, which has the following default style trigger.

 <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> 

So, if you are writing a trigger for a DataGridRow, then it will only apply to the element that was placed before the DataGridCell in the visual tree.

So, in order to change the Background and Foreground during the selection, you have to write a trigger in the DataGridCell style or remove the default trigger from the style.

0
source

Just remove these row level attributes in the datagrid, they take precedence over the running properties.

nezac

0
source

All Articles