C# WPF:在运行时动态更改DataGrid单元格/行背景色

C# WPF:在运行时动态更改DataGrid单元格/行背景色,c#,wpf,xaml,dynamic,datagrid,C#,Wpf,Xaml,Dynamic,Datagrid,我有多个绑定到数据表的数据网格,这些数据表是使用SQL动态创建的。每当DataTable记录更改(添加、修改、删除)时,DataGridCell应相应更改其背景色(绿色=新,黄色=修改等) 在WinForms中,我使用RowPostPaint更改了DataGridView的背景色(代码非常简化): 我不想像中那样硬编码XAML中的列依赖项,因为它们是在运行时创建的,而且我使用了许多数据网格 尝试使用DataGrid_CellEditEnding失败,因为它不会在排序等时保留更改: XAML: 这

我有多个绑定到数据表的数据网格,这些数据表是使用SQL动态创建的。每当DataTable记录更改(添加、修改、删除)时,DataGridCell应相应更改其背景色(绿色=新,黄色=修改等)

在WinForms中,我使用RowPostPaint更改了DataGridView的背景色(代码非常简化):

我不想像中那样硬编码XAML中的列依赖项,因为它们是在运行时创建的,而且我使用了许多数据网格

尝试使用DataGrid_CellEditEnding失败,因为它不会在排序等时保留更改:

XAML:

这会很好地改变背景颜色,但是在对数据网格等进行排序时不会保留样式

如何确保保持颜色变化?在我看来,最好的解决方案是以某种方式将DataRows绑定到helper类,并在DataTable更改时返回相应的样式。但是,我还没有看到任何这样的例子。

完整性:

如果您真的打算在运行时更改颜色,而不考虑MVVM,您可以使用DataGrid_LoadingRow,检查其DataContext(在本例中为DataRowView),然后继续:

// Changes beeing made to the entire row in this case
private void DgModules_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGridRow gridRow = e.Row;
    DataRow row = (gridRow.DataContext as DataRowView).Row;
    switch (row.RowState)
    {
        case DataRowState.Added:
            gridRow.Background = new SolidColorBrush(Colors.Green);
            break;
        case DataRowState.Modified:
            gridRow.Background = new SolidColorBrush(Colors.Yellow);
            break;
        case DataRowState.Deleted:
            gridRow.Background = new SolidColorBrush(Colors.Red);
            break;
    }
}

如果你真的想用MVVM来解决这个问题,那就去吧。

我想这会很难。如果不将
dataTable.DefaultView
包装到管理单元格当前状态的帮助器类中,我就看不到这一点。我的猜测是,对DataGrid重新排序会刷新所有绘制的单元格,因为DataGrid是唯一知道如何绘制特定单元格的单元格,所以它可能会忘记。DataGrid需要从数据中知道如何给每个单元格着色。@MarkusHütter是的,这也是我的想法,我基本上知道如何为特定的类或硬编码的XAML使用帮助器类。我还没有看到任何处理我的问题的例子,我一直在思考如何为绑定的数据表使用helper类。
<Window.Resources>
    <Style x:Key="MyStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Green" />
    </Style>
</Window.Resources>
<DataGrid x:Name="dataGrid" Grid.Row="4" Grid.ColumnSpan="6"
    ItemsSource="{Binding}"
>
</DataGrid>
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
        }
    }
// Changes beeing made to the entire row in this case
private void DgModules_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGridRow gridRow = e.Row;
    DataRow row = (gridRow.DataContext as DataRowView).Row;
    switch (row.RowState)
    {
        case DataRowState.Added:
            gridRow.Background = new SolidColorBrush(Colors.Green);
            break;
        case DataRowState.Modified:
            gridRow.Background = new SolidColorBrush(Colors.Yellow);
            break;
        case DataRowState.Deleted:
            gridRow.Background = new SolidColorBrush(Colors.Red);
            break;
    }
}