ewall ewall - 2 months ago 43
C# Question

Loop through all cells in Xceed DataGrid for WPF?

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?

Answer

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;
    }
}