WPF: Change DataGrid cell/row background color dynamically at runtime
<p>I have multiple DataGrids bound to DataTables, which are dynamically created using SQL. Whenever DataTable records change (add, modify, delete), the DataGridCells shall change their background color accordingly (green=new, yellow=modify etc.).</p>

<p>In WinForms I changed DataGridView's background color using _RowPostPaint (code is very simplified):</p>

<pre><code>private void DataGridViewTest_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    DataRow row = (this.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
    switch (row.RowState)
    {
        case DataRowState.Added:
            myBitmap = new Bitmap(imageList.Images[1]);
            this[0, e.RowIndex].Style.BackColor = CellChangesColorAdded;
            break;
        case DataRowState.Modified:
            string sValOld = row[0, DataRowVersion.Original].ToString();
            string sValNew = row[0].ToString();
            if (sValOld != sValNew)
            {
                this[0, e.RowIndex].Style.BackColor = CellChangesColorMod;
            }
            break;
        case DataRowState.Deleted:
            this[0, e.RowIndex].Style.BackColor = CellChangesColorDel;
            break;
    }
}
</code></pre>

<p>I do not want to hardcode column dependencies in XAML as in <a href="https://stackoverflow.com/questions/32296339/wpf-datagrid-updating-cell-style-after-editing">countless examples like this</a> as they are created at run time and I have many DataGrids in use.</p>

<p>Trying to use DataGrid_CellEditEnding fails, as it does not keep the changes upon sorting etc.:</p>

<p>XAML:</p>

<pre><code>&lt;Window.Resources&gt;
    &lt;Style x:Key="MyStyle" TargetType="{x:Type DataGridCell}"&gt;
        &lt;Setter Property="Background" Value="Green" /&gt;
    &lt;/Style&gt;
&lt;/Window.Resources&gt;
&lt;DataGrid x:Name="dataGrid" Grid.Row="4" Grid.ColumnSpan="6"
    ItemsSource="{Binding}"
&gt;
&lt;/DataGrid&gt;
</code></pre>

<p>.cs:</p>

<pre><code>dataGrid.DataContext = dataTable.DefaultView; // Table filled by SQL query
dataGrid.CellEditEnding += dataGrid_CellEditEnding;

// Problem: Color changes disappear when user sorts DataGrid
    private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if (e.EditAction == DataGridEditAction.Commit)
        {
            TextBox tb = e.EditingElement as TextBox;
            DataGridCell cell = tb.Parent as DataGridCell;
            // evaluate row changes and change color accordingly
            //cell.Background = new SolidColorBrush(Colors.Yellow); // set style instead of color
            cell.Style = (Style)this.Resources["MyStyle"]; // color is changed to green, according to defined style
        }
    }
</code></pre>

<p>This changes the background color perfectly fine, however the style is not being kept upon sorting the DataGrid etc.</p>

<p>How can I ensure color changes are being kept? In my opinion, the best solution would be to somehow bind DataRows to a helper class and return the respective style upon DataTable changes. However I haven't seen any example for that yet.</p>