c# - 数据网格取消编辑不起作用

标签 c# wpf datagrid

我的 DataGrid 是在 XAML 中定义的:

<datagrid:ThemedDataGrid AutoGenerateColumns="False" ItemsSource="{Binding Model.ItemCollection, UpdateSourceTrigger=PropertyChanged}"  
                 SelectionUnit="FullRow" SelectedItem="{Binding Model.DatagridSelectedItem, UpdateSourceTrigger=PropertyChanged}">
</datagrid:ThemedDataGrid>

我有一个事件RowEditEnding,我在其中检查列中是否已存在与先前输入的值相同的单元格。如果存在,那么我需要取消编辑。我的 RowEditEnding 方法如下:

int counter = 0;
Model.ItemCollection.ForEach(x =>
{
    //if column is not empty
    if (!String.IsNullOrEmpty(x.Name))
    {
        if (x.Name== Model.DatagridSelectedItem.Name)
        {
            counter++;
            if (counter > 1)
            {
                MessageBox.Show("Doubled Name");
                e.Cancel = true;
                datagrid.CancelEdit(DataGridEditingUnit.Row);                                                            
            }
        }
    }
});

问题出在这一行:

datagrid.CancelEdit()

这不会将单元格值更改为前一个值,并且我会陷入无限循环。怎么解决呢?

最佳答案

您的集合是否绑定(bind)到使用自定义对象填充的 ItemsSource 属性?如果是这样,我认为您的自定义数据类必须实现 IEditableObject界面。 确实来自DataGrid documentation :

To guarantee that edits can be committed and canceled correctly, the objects in the DataGrid must implement the IEditableObject interface.

关于c# - 数据网格取消编辑不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880866/

相关文章:

c# - 存储和检索在使用 facebook 或 twitter 授权后为用户收到的 token

c# - Entity Framework 4.1 Code First 和 Oracle CLOB

c# - 在 VSCode 中构建 C# 不起作用

c# - 如何让子窗口与主窗口一起最小化和最大化

javascript - 如何在 dojo.grid.DataGrid 中创建具有不同小部件的列?

C# Winform 数据网格增删改查

C# 泛型列表

c# - 如何在 Azure Fluent API for SQL Server 中为现有数据库设置服务器级别目标(定价层)?

wpf - 简单 WPF ItemsControl 的奇怪焦点行为

WPF Datagrid,是否可以混合使用组和普通(非分组)行?