WPF Toolkit DataGridCell Style DataTrigger

大兔子大兔子 提交于 2019-12-05 18:46:52

The reason this happens is because DataContext is set at row level and doesn't change for each DataGridCell. So when you bind to IsDirty it binds to the property of row-level data object, not cell-level one.

Since your example shows that you have AutoGenerateColumns set to false, I assume that you generate columns yourself have something like DataGridTextColumn with Binding property set to binding to actual value field. To get cell style changed to yellow you'd have to change CellStyle on each DataGridColumn like this:

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 DataContext of each cell using DataGridColumn.CellStyle. Perhaps only then you'll be able to bind cell's to 'IsDirty' directly from grid-level style like you do without doing it for each column individually. But I don't have actual data model you have to test this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!