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