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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38036580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:51:34  来源:igfitidea点击:

WPF: Change DataGrid cell/row background color dynamically at runtime

c#wpfxamldynamicdatagrid

提问by ray

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.).

我有多个绑定到 DataTables 的 DataGrids,它们是使用 SQL 动态创建的。每当 DataTable 记录更改(添加、修改、删除)时,DataGridCell 应相应地更改其背景颜色(绿色 = 新,黄色 = 修改等)。

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

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

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

I do not want to hardcode column dependencies in XAML as in countless examples like thisas they are created at run time and I have many DataGrids in use.

我不想在 XAML 中对列依赖项进行硬编码,因为它们是在运行时创建的,而且我有许多 DataGrid 正在使用,因此在无数这样的示例中。

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

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

XAML:

XAML:

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

.cs:

。CS:

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

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

这将完美地更改背景颜色,但是在对 DataGrid 等进行排序时不会保留样式。

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.

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

回答by ray

For completeness:

为了完整性:

If you really intend to change the color at runtime, disregarding MVVM you can for instance use DataGrid_LoadingRow, check for it's DataContext (in this case a DataRowView) and go on from there:

如果您真的打算在运行时更改颜色,不考虑 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;
    }
}

If you wanna approach this actually using MVVM, go for this solution.

如果您想实际使用 MVVM 来解决这个问题,请选择此解决方案