Change Row Color in WPF DataGrid Control

Jan 30, 2009 at 3: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 6:28 PM
Anyone know the answer to this?
Feb 3, 2009 at 10:13 PM
Edited Feb 3, 2009 at 10: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}" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding ToBeHighlighted}" Value="true">
            <Setter Property="Background" Value="LightYellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>
Feb 5, 2009 at 11:56 AM
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 3:30 PM
Hi,

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:

<cite></cite>http://www.codeproject.com/KB/WPF/HiliteListViewItemsInWPF.aspx

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.

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples