How can I set the color of a selected row in DataGrid
The above solution left blue border around each cell in my case.
This is the solution that worked for me. It is very simple, just add this to your DataGrid
. You can change it from a SolidColorBrush
to any other brush such as linear gradient.
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#FF0000"/>
</DataGrid.Resources>
Got it. Add the following within the DataGrid.Resources section:
<DataGrid.Resources>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
As an extention to @Seb Kade's answer, you can fully control the colours of the selected and unselected rows using the following Style
:
<Style TargetType="{x:Type DataGridRow}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
You can of course enter whichever colours you prefer. This Style
will also work for other collection items such as ListBoxItem
s (if you replace TargetType="{x:Type DataGridRow}"
with TargetType="{x:Type ListBoxItem}"
for instance).