在选项卡控件中处理多个数据网格以便单元格在选项卡更改时离开编辑模式的正确方法是什么?
在 wpf 中,我设置了一个选项卡控件,该控件绑定到对象集合,每个对象都有一个数据模板,其中包含显示数据的数据网格。如果我选择一个特定的单元格并将其置于编辑模式,通过转到另一个选项卡来离开网格,这将导致在返回数据网格时引发以下异常:
在 AddNew 或 EditItem 事务期间不允许“DeferRefresh”。
该单元格似乎从未离开过编辑模式。有没有一种简单的方法可以使单元格退出编辑模式,或者这里发生了其他事情?
更新:看起来,如果我不将选项卡控件绑定到数据源,而是显式定义每个选项卡,然后将数据源中的每个项目绑定到内容控件,那么这个问题就会消失。这并不是一个很好的解决方案,所以我仍然想知道如何将集合直接绑定到选项卡控件。
更新:因此,我实际上为自己的解决方案所做的是使用 ListView 和内容控件来代替选项卡控件。我使用一种样式使列表视图看起来像选项卡。视图模型公开一组子视图模型,并允许用户通过列表视图选择一个。然后,内容控件呈现选定的视图模型,每个视图模型都有一个包含数据网格的关联数据模板。通过此设置,在网格上处于编辑模式时在视图模型之间切换将正确结束编辑模式并保存数据。
这是用于设置此功能的 xaml:
<ListView ItemTemplate="{StaticResource MakeItemsLookLikeTabs}"
ItemsSource="{Binding ViewModels}"
SelectedItem="{Binding Selected}"
Style="{StaticResource MakeItLookLikeATabControl}"/>
<ContentControl Content="{Binding Selected}">
我会接受 Phil 的答案,因为这也应该有效,但对我来说,上面的解决方案似乎在项目之间更易于移植。
In wpf I setup a tab control that binds to a collection of objects each object has a data template with a data grid presenting the data. If I select a particular cell and put it into edit mode, leaving the grid by going to another tab this will cause the exception below to be thrown on returning the datagrid:
'DeferRefresh' is not allowed during an AddNew or EditItem transaction.
It appears that the cell never left edit mode. Is there an easy way to take the cell out of edit mode, or is something else going on here?
Update: It looks like if I do not bind the tab control to the data source, but instead explicitly define each tab and then bind each item in the data source to a content control this problem goes away. This is not really a great solution, so I would still like to know how to bind the collection directly to the tab control.
Update: So what I have actually done for my own solution is to use a ListView and a content control in place of a tab control. I use a style to make the list view look tab like. The view model exposes a set of child view models and allows the user to select one via the list view. The content control then presents the selected view model and each view model has an associated data template which contains the data grid. With this setup switching between view models while in edit mode on the grid will properly end edit mode and save the data.
Here is the xaml for setting this up:
<ListView ItemTemplate="{StaticResource MakeItemsLookLikeTabs}"
ItemsSource="{Binding ViewModels}"
SelectedItem="{Binding Selected}"
Style="{StaticResource MakeItLookLikeATabControl}"/>
<ContentControl Content="{Binding Selected}">
I'll accept Phil's answer as that should work also, but for me the solution above seems like it will be more portable between projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我根据在 这个线程。
用法:
代码:
I implemented a behavior for the DataGrid based on code I found in this thread.
Usage:
<DataGrid local:DataGridCommitEditBehavior.CommitOnLostFocus="True" />
Code:
我已经成功解决了这个问题,方法是检测用户何时单击
TabItem
,然后在TabControl
中的可见DataGrid
上提交编辑。我假设用户希望当他们点击返回时他们的更改仍然存在。代码片段:
这是在后面的代码中。
I have managed to work around this issue by detecting when the user clicks on a
TabItem
and then committing edits on visibleDataGrid
in theTabControl
. I'm assuming the user will expect their changes to still be there when they click back.Code snippet:
This is in the code behind.
此错误在 .NET Framework 4.5 中已得到解决。您可以通过此链接下载它。
This bug is solved in the .NET Framework 4.5. You can download it at this link.
我认为你应该做的与@myermian 所说的非常接近。
有一个名为 CellEditEnding end 的事件,该事件允许您拦截并决定删除不需要的行。
What I think you should do is pretty close to what @myermian said.
There is an event called CellEditEnding end this event would allow you to intercept and make the decision to drop the unwanted row.