问题描述
In the same series as the DataGrid validation scheme issues , I get an exception when
- 单元格无效
- DataContext更改
这发生在绑定到CollectionViewSource的DataGrid中.此CVS本身具有绑定到ListBox当前选定产品的DataContext.因此,我们正在编辑产品的详细信息,这些详细信息包括DataGrid(DataGridTextColumn)中的数据列表. 在这种情况下).
This happens in a DataGrid, which is bound to a CollectionViewSource. This CVS itself has a DataContext bound to the currently selected product of a ListBox. So we are editing the details of product, those details include a data list in a DataGrid (DataGridTextColumn in this case).
通过使用实现IDataErrorInfo的数据类型或通过ValidationRule,如果验证失败,则DataGrid会通过保留TextBox并在其周围显示一个红色框来做出反应(当验证正常时,单元格将转换回一个 TextBlock).
Either by using a data type that implements IDataErrorInfo or by using a ValidationRule, if the validation fails the DataGrid reacts by keeping the TextBox and showing a red box around it (when the validation is OK, the cell is tranformed back to a TextBlock).
没有什么可以阻止我选择其他产品.然后,DataGrid显示新数据,一切正常.但是,如果我返回验证失败的产品,则会收到此异常:
Nothing prevents me from selecting another product. The DataGrid then shows the new data and everything is fine. But if I come back to the product which failed validation, I get this exception:
InvalidOperationException,在AddNew或EditItem事务期间不允许使用'DeferRefresh'.
如何防止这种情况?选择其他产品或DataContext更改后,取消对该故障单元格上的编辑的正确方法是什么?
How can I prevent that? What is the correct way of cancelling the edit on that faulty cell when another product is selected, or when the DataContext changes?
我可以检测到那些事件,但是CancelEdit()在DataGrid上不起作用(无结果).
I can detect those events, but CancelEdit() doesn't work on the DataGrid (no result).
我正在使用.NET 4.
I'm using .NET 4.
可以在此处查看代码: C#部分, XAML部分.可以下载代码从此处.
The code can be viewed here: C# part ,XAML part . The code can be downloadedfrom here .
推荐答案
这篇关于验证失败时出现DataGrid异常(不允许使用DeferRefresh ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!