我的 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/