Change Row Color in WPF DataGrid Control

Jan 30, 2009 at 4:03 PM
How do you change the Foreground color of an entire row in WPF DataGrid control via code?  Meaning,  I have found an item in datagrid by searching the item's ID ( or other column parameter ).  Now how do I change the row color the item is located in the datagrid?  Thanks in advance.
Feb 3, 2009 at 7:28 PM
Anyone know the answer to this?
Feb 3, 2009 at 11:13 PM
Edited Feb 3, 2009 at 11:13 PM
Hello shaggygi,

You can define a DataTrigger on the DataGridRow that will change the foreground color if a property has a specific value.         

<Style TargetType="{x:Type dg:DataGridRow}" >
        <DataTrigger Binding="{Binding ToBeHighlighted}" Value="true">
            <Setter Property="Background" Value="LightYellow" />
Feb 5, 2009 at 12:56 PM
Thanks for the reply.  I'm new to the WPF DataGrid and a little confused on your solution.  Do you have a basic example to demo this?  I have my data bound to an ObservableCollection and would like to change the color of entire row where data was changed in the item.  Thanks again.
Feb 5, 2009 at 4:30 PM

The example which pikul gave uses a data trigger. These are triggers that enable style property setters based on some property of your object. See this codeproject article for a good tutorial of this technique:


You want to highlight a row when the data has changed? In this case you will have to provide a property on your data objects to support this, i.e. a boolean IsChanged property that you can bind to in your data trigger.

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples