Reputation: 595
How to: Change the DataGrid IsSelected Background (C#, WPF)
I do have the following code in my App.xaml
:
<Application.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Orange" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="WhiteSmoke" />
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
Unfortunately, the following result is what I get:
What do I need to do to fix my Problem?
Upvotes: 0
Views: 258
Reputation: 8802
You can add this bit of Xaml to your data grid resources...
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
</DataGrid.Resources>
The two brushes work together to set the background/foreground when a row is selected.
To set the default colours for a DataGrid row, you can add this snippet...
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="Pink" />
<Setter Property="Foreground" Value="DodgerBlue"/>
</Style>
</DataGrid.RowStyle>
This sets the background to Pink and the Foreground to DodgerBlue.
More info on the SystemColors static resource is at http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx
Upvotes: 1
Reputation: 2031
You have to set the style for IsSelected
property on DataGridCell
as it is (apparently) rendered after DataGridRow
.
Upvotes: 0