datagrid: how to change style of individual rows

Mar 8, 2009 at 11:16 AM
Hi all,

i'd like to change the color of individual rows based on a property in the bound datasource.

Suppose i've got a collection (observable of course) with a few items and each item has a boolean property. The itemssource prop of the datagrid is set to this collection.
I would really like set the color of each row based on the boolean value of each item. Up till now i can't seem to find the solution. I've used rowbackground with a converter, but that only changes each and every row. Anu suggestions?

On a sidenote. The defult backgroundcolor of the rows is defined in a generic.xaml file. When i use a converter to change this color, what do i have to do in the convertback method? Right now i return a null value which causes the rows to set back to some gray color instead of the one in my generic.xaml.

Meanwhile i'm of to look at the frozenrow sample. Maybe there's a solution for me.
Thanks in advance!

Mar 8, 2009 at 12:21 PM
Apparently there is a property called rowstyle, so the answere is as fast as it is simple (thanks to google and a few found posts of others).
With the code below the main issue of the OP is solved. All that remains now is when i uncheck the checkbox, the color of the row doesn't go back to its original color, but it will become a nasty shade of gray.
So i can still use some help on that one.
Currently i return null in the ConvertBack method. I did try to set the mode to oneWay on the background property, did not do any good.

Anyway, here's the simple xaml which i found.


<wpf:DataGrid ItemsSource="{Binding Path=Employees}" AutoGenerateColumns="False">






<wpf:DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Name}" Header="Naam" MinWidth="110"></wpf:DataGridTextColumn>



<wpf:DataGridCheckBoxColumn IsReadOnly="False" Binding="{Binding Path=IsActive}" Header="Actief" MinWidth="50"></wpf:DataGridCheckBoxColumn>









<Style TargetType="{x:Type wpf:DataGridRow}">



<Setter Property="Background"



Value="{Binding IsActive, Converter={StaticResource BackgroundColorConverter}, Mode=oneWay}" />










Mode=oneWay is'nt really necessary here. The name of the converter really should be something like BooleanToBackgroundConverter or something.




Mar 8, 2009 at 1:03 PM
well, for problem two, Paul Stovell has a nice solution. He writes about it in

Now all i need is someone to translate this solution to my problem. I am using a generic.xaml in which a style for the datagrid is defined.