wpf 在 AddNew 或 EditItem 事务期间不允许使用“DeferRefresh”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18281615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'DeferRefresh' is not allowed during an AddNew or EditItem transaction
提问by yinyang
I have a tab control in the GUI and there is WPF 4.0 datagrid in one of the tabs. When I click on a cell in the grid and edit something and then switch tabs, I get a Defer Refresh error:
我在 GUI 中有一个选项卡控件,其中一个选项卡中有 WPF 4.0 数据网格。当我单击网格中的单元格并编辑某些内容然后切换选项卡时,出现延迟刷新错误:
DeferRefresh' is not allowed during an AddNew or EditItem transaction.
在 AddNew 或 EditItem 事务期间不允许 DeferRefresh'。
So I call datagrid.CancelEdit(DataGridEditingUnit.Row)when tab is switched to cancel any pending edit and the Defer refresh issue is gone.
所以我datagrid.CancelEdit(DataGridEditingUnit.Row)在切换选项卡时调用以取消任何挂起的编辑并且延迟刷新问题消失了。
But what I really want to do is CommitEdit()so that the user doesn't have to reenter the data again.
但我真正想做的是CommitEdit()让用户不必再次重新输入数据。
And datagrid.CommitEdit(DataGridEditingUnit.Row, true)doesn't work for me.
I get the below error on CommitEnd():
而且datagrid.CommitEdit(DataGridEditingUnit.Row, true)对我不起作用。我收到以下错误CommitEnd():
Cannot perform this operation while dispatcher processing is suspended.
调度程序处理暂停时无法执行此操作。
PS: I have tried datagrid.CommitEdit() and datagrid.CommitEdit(DataGridEditingUnit.Column, true) and it didnt work.
PS:我试过 datagrid.CommitEdit() 和 datagrid.CommitEdit(DataGridEditingUnit.Column, true) 但没有用。
回答by Foole
I solved this by adding this handler for the DataGrid's Unloaded event:
我通过为 DataGrid 的 Unloaded 事件添加这个处理程序解决了这个问题:
void DataGrid_Unloaded(object sender, RoutedEventArgs e)
{
var grid = (DataGrid)sender;
grid.CommitEdit(DataGridEditingUnit.Row, true);
}
回答by Patrick Y
I've run in to this before. WPF only keeps the current tab's view in memory; when you switch tabs, WPF unloads the current view and loads the view of the selected tab. However, DataGrid throws this exception if is currently executing a AddNew or EditItem transaction and WPF tries to unload it.
我以前遇到过这个。WPF 只在内存中保留当前选项卡的视图;当您切换选项卡时,WPF 会卸载当前视图并加载所选选项卡的视图。但是,如果当前正在执行 AddNew 或 EditItem 事务并且 WPF 尝试卸载它,则 DataGrid 会引发此异常。
The solution for me was to keep all the tab views in memory, but only set the current tab's view to visible. This link shows a method of doing this:
我的解决方案是将所有选项卡视图保留在内存中,但仅将当前选项卡的视图设置为可见。此链接显示了执行此操作的方法:
WPF TabControl - Preventing Unload on Tab Change?
This change will also make your tabs load more smoothly when you switch between them because the view doesn't have to be regenerated. In my case, the extra memory usage was a reasonable trade-off.
当您在它们之间切换时,此更改还将使您的选项卡加载更顺畅,因为不必重新生成视图。就我而言,额外的内存使用是一个合理的权衡。
回答by Bast73
I just solved a similar problem by "commiting" changes to the DataTable that is my source of datas.
我刚刚通过对作为我的数据源的 DataTable 进行“提交”更改来解决类似的问题。
So if you have a DataTable in the source you can try the following code :
因此,如果源中有 DataTable,则可以尝试以下代码:
DataTableSource.AcceptChanges();
回答by Bahaa Salaheldin
I've fixed that issue by adding this piece of code:
我通过添加这段代码解决了这个问题:
private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tabControl.SelectedIndex == 1)
{
WPFdataGrid.CancelEdit(DataGridEditingUnit.Row);
}
}
I think it's a problem of UI threads.
我认为这是UI线程的问题。
回答by Cedre
In Xaml :
在 Xml 中:
Loaded="OnUserControlLoaded"
Unloaded="OnUserControlUnloaded"
In Code Behind Inside OnUserControlLoadedand OnUserControlUnloadedMethods:
在内部OnUserControlLoaded和OnUserControlUnloaded方法背后的代码中:
dataGrid.CommitEdit()
dataGrid.CancelEdit()

