简体   繁体   English

突出显示WPF DataGrid的已编辑单元格

[英]Highlight edited cells of WPF DataGrid

I am currently working on an app displaying data I am querying from a database into a DataGrid. 我目前正在开发一个应用程序,用于显示我正在从数据库查询到DataGrid的数据。

As the columns of the table are not know in advanced, I was not able to implement the DataGrid as an ObservableCollection of objects, and the DataGrid is consequently binded to a DataTable: 由于表的列在高级方面是未知的,因此我无法将DataGrid实现为对象的ObservableCollection,因此DataGrid绑定到了DataTable:

MyDataGrid.ItemsSource = _myDataTable.DefaultView;

Users should be able to edit data directly in the DataGrid, and the edited cells should be highlighted. 用户应该能够直接在DataGrid中编辑数据,并且应突出显示已编辑的单元格。

At the moment, the only progress I was able to make on that matter was to use the CellEditEnding event to change the color of the cell: 目前,我在此问题上唯一能做的就是使用CellEditEnding事件来更改单元格的颜色:

private void MyDataGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        DataGridCell gridCell = null;
        if (sender is DataGrid dg)
        {
            gridCell = GetCell(dg.CurrentCell);
        }

        if (gridCell != null)
            gridCell.Foreground = Brushes.Red;

    }
}

public DataGridCell GetCell(DataGridCellInfo dataGridCellInfo)
{
    if (!dataGridCellInfo.IsValid)
    {
        return null;
    }

    var cellContent = dataGridCellInfo.Column.GetCellContent(dataGridCellInfo.Item);
    return (DataGridCell) cellContent?.Parent;
}

This approach works well if a user edits a cell by double-clicking on it, changing the value and pressing enter to commit the changes. 如果用户通过双击单元格,更改值并按Enter提交更改来编辑单元格,则此方法效果很好。

However, it fails if the user edits the cell and commits the editing by clicking on a new row. 但是,如果用户编辑单元格并通过单击新行来提交编辑,则失败。 In this case, the new cell is colored rather than the edited one, which makes sense as dg.CurrentCell evaluates to the new selected cell. 在这种情况下,新单元格是彩色的,而不是已编辑的单元格,这对于dg.CurrentCell是有意义的dg.CurrentCell求值到新选择的单元格。

What are possible leads to color the edited cell rather than the newly selected cell? 有什么可能导致编辑的单元格而不是新选择的单元格着色?

Do you know of a better approach to highlight edited cells from a DataGrid bonded to a DataTable? 您是否知道从绑定到DataTable的DataGrid中突出显示已编辑单元格的更好方法?

Like you said, using the CurrentCell won't work because it changes when selecting a different cell. 就像您说的那样,使用CurrentCell无效,因为在选择其他单元格时它会更改。 The OnCellEditEnding event provides the edited element in the event arguments but you need to get the DataGridCell that it is contained in to change cell properties. OnCellEditEnding事件在事件参数中提供了已编辑的元素,但是您需要获取其中包含的DataGridCell才能更改单元格属性。 I too wish it would just be provided but you need to walk up the visual tree to get it: 我也希望只提供它,但是您需要走到视觉树上才能得到它:

private void MyDataGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
            DependencyObject depObj = e.EditingElement;
            while (depObj != null && !(depObj is DataGridCell)) {
                depObj = VisualTreeHelper.GetParent (depObj);
            }

            if (depObj != null) {
                DataGridCell gridCell = (DataGridCell) depObj;
                gridCell.Foreground = Brushes.Red;
            }
    }
}

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

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