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



我有多个绑定到数据塔的数据,这些datagrids是使用SQL动态创建的。每当DataTable记录更改(添加,修改,删除)时,DataGridCells应相应地更改其背景颜色(green = new,Yellow = Modify等)。

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

我不想在XAML中的硬码列依赖项,因为在运行时创建的无数示例中,我使用了许多datagrids。

尝试使用datagrid_celleditending失败,因为它不会在排序时保持更改等。

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:

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

这完全更改了背景颜色,但是在对数据杂志等进行分类时,样式并未保持样式。

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

用于完整性:

如果您真的打算在运行时更改颜色,则无视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来处理此解决方案。

最新更新