This seems like a no-brainer but i just can't see how to do it.
The default background color of a selected row in DataGrid is so dark that I can't read it. Is there anyway of overriding it?
Tried this (modified from Neverminds link)
<dg:DataGrid.RowStyle>
<Style TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="Gainsboro" />
</Trigger>
</Style.Triggers>
</Style>
</dg:DataGrid.RowStyle>
But still nothing...
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 aSolidColorBrush
to any other brush such as linear gradient.Got it. Add the following within the DataGrid.Resources section:
I had this problem and I nearly tore my hair out, and I wasn't able to find the appropriate answer on the net. I was trying to control the background color of the selected row in a WPF DataGrid. It just wouldn't do it. In my case, the reason was that I also had a CellStyle in my datagrid, and the CellStyle overrode the RowStyle I was setting. Interestingly so, because the CellStyle wasn't even setting the background color, which was instead bing set by the RowBackground and AlternateRowBackground properties. Nevertheless, trying to set the background colour of the selected row did not work at all when I did this:
and it did work when I moved the desired style for the selected row out of the row style and into the cell style, like so:
Just posting this in case someone has the same problem.
Some of the reason which I experienced the row selected event not working
This is what helped me. Setting the Style for DataGridCell
And since I was using a template column with a label inside, I bound the Foreground property to the container Foreground using RelativeSource binding:
As an extention to @Seb Kade's answer, you can fully control the colours of the selected and unselected rows using the following
Style
:You can of course enter whichever colours you prefer. This
Style
will also work for other collection items such asListBoxItem
s (if you replaceTargetType="{x:Type DataGridRow}"
withTargetType="{x:Type ListBoxItem}"
for instance).The default IsSelected trigger changes 3 properties, Background, Foreground & BorderBrush. If you want to change the border as well as the background, just include this in your style trigger.