views:

397

answers:

2

I am changing the background color of the cells when the user has made an edit. I would like to return all cells to normal colors when the changes are saved (or reverted).

It's easy enough to set the cell's original background color (as stored in the parent row). But I can't figure out how to loop through all the cells in the table to reset them.

I found an article in the Xceed Knowledge Base called "How to iterate through the grid's rows"... which you would think would be perfect, right? Wrong; the properties (or methods) like .DataRows, .FixedHeaderRows, etc. mentioned in the article are from an older/defunct Xceed product.

This forum thread recommends using the DataGrid's .Items property, which in my case returns a collection of System.Data.DataRowViews... but I can't find any way to cast that (or any of its related elements) up to the Xceed.Wpf.DataGrid.DataCells I need to change the background color.

In short, how do I loop through the rows and cells so I can reset the background property?

A: 

I propose that you change your business logic to utilize data binding instead.

Each cell in your data grid would then be an object, which itself knows if it has been edited or not. And then you can data bind to that property, and therefore when you save and reset all of your objects, the status will also be updated in your gui.

Also, you get a separation of concerns for free. You GUI now decides how things should LOOK, not what the business logic of tracking saved/not saved should be.

Cine
My DataGrid is already using data binding. So yes, the business logic and presentation are already separated, and this question is only about the presentation part of it.I didn't mention in the question that I have already tried creating a separate style (in essence, "highlighted") when the cell's `IsDirty` property is true... but it doesn't work. As far as I can tell, Xceed's DataGrid for WPF only allows me to set style triggers for some properties (e.g. `IsMouseOver`) but not others (including data-related ones like `IsDirty`).
ewall
+2  A: 

The question has been resolved, thanks to Mohamed, an Xceed employee who posted on the Xceed Forums. Example code follows:

foreach (object item in this.DataGrid1.Items)
{
    Dispatcher.BeginInvoke(new Action<object>(RemoveRowHighlights), DispatcherPriority.ApplicationIdle, item);
}
...
private void RemoveRowHighlights(object item)
{
    Xceed.Wpf.DataGrid.DataRow row = this.DataGrid1.GetContainerFromItem(item) as Xceed.Wpf.DataGrid.DataRow;
    if (row != null) foreach (Xceed.Wpf.DataGrid.DataCell c in row.Cells)
    {
        if (c != null) c.Background = row.Background;
    }
}
ewall