The reason for this is because the DataContext set at the row level and does not change for each DataGridCell . Therefore, when you bind to IsDirty , it binds to a row-level data object property, not the first level.
Since your example shows that you have AutoGenerateColumns set to false, I assume that you yourself create columns that have something like DataGridTextColumn with the Binding attribute set to bind to the actual value field. To change the cell style to yellow, you need to change the CellStyle to each DataGridColumn as follows:
foreach (var column in columns) { var dataColumn = new DataGridTextColumn { Header = column.Caption, Binding = new Binding(column.FieldName), CellStyle = new Style { TargetType = typeof (DataGridCell), Triggers = { new DataTrigger { Binding = new Binding(column.FieldName + ".IsDirty"), Setters = { new Setter { Property = Control.BackgroundProperty, Value = Brushes.Yellow, } } } } } }; _dataGrid.Columns.Add(dataColumn); }
You can experiment with changing the DataContext each cell using the DataGridColumn.CellStyle . Perhaps only then can you bind a cell to "IsDirty" directly from the style at the grid level, just like you, without doing this for each column separately. But I do not have a real data model that you have to check.
repka source share