繁体   English   中英

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

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

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

在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中的列依赖关系进行硬编码,因为它们是在运行时创建的,并且我使用了许多DataGrid。

尝试使用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
        }
    }

这样可以很好地更改背景颜色,但是在对DataGrid等进行排序时不会保留样式。

如何确保保持颜色变化? 我认为,最好的解决方案是以某种方式将DataRows绑定到助手类,并在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来解决此问题 ,请使用此解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM