如果在 TabControl 上编辑和切换选项卡,DataGrid 的新行会消失
我的应用程序中有一个通过 XAML 创建的 WPF TabControl 对象。同样通过 XAML 创建的是一个包含 DataGrid 的 TabItem。在我的应用程序中,用户可以为该 TabControl 创建新选项卡。发生这种情况时,将为该新 TabItem 创建一个 DataGrid。因此,应用程序最终可能会包含多个带有 DataGrid 的 TabItem,即使我只通过 XAML 创建一个带有 DataGrid 的 TabItem。
我看到一个问题,如果用户想要在 DataGrid 中添加新行,但随后决定切换到其他选项卡,则当用户返回到该选项卡时,DataGrid 会丢失新行。因此不可能向 DataGrid 添加新行。奇怪的是,这个问题只发生在为动态 TabItem 动态创建的 DataGrid 上。因此,通过 XAML 创建的 DataGrid 中不存在此问题。以前有人见过这个问题吗?
I have a WPF TabControl object in my application created through XAML. Also created through XAML, is one TabItem that contains a DataGrid. In my application, the user can create new Tabs for that TabControl. When this happens, a DataGrid is created for that new TabItem. So the application could end up containing several TabItems with DataGrids, even though I only create one TabItem with a DataGrid through XAML.
I am seeing an issue that if the user wants to add a new row in the DataGrid, but then decides to switch to a different Tab, the DataGrid is missing the new row when the user returns to that Tab. So then it is impossible to add new rows to the DataGrid. The weird thing is, is that this issue happens only on the DataGrids that are dynamically created for the dynamic TabItems. So this issue is not present in the DataGrid that was created through XAML. Has anyone seen this issue before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您需要在更改选项卡之前提交网格中的所有编辑。这是一个我发现非常有用的很好的解决方法:
It appears that you need to commit all your edits in the grid before changing tabs. Here is a nice workaround that I found quite useful:
结果发现 Stack Overflow 上有一个与此非常相似的问题。这是它的链接。接受的答案是为我解决问题的答案。
带数据网格的 TabControl
Turn out there is a question that was very similar to this one here on Stack Overflow. Here is a link to it. The accepted answer is the one that resolved the issue for me.
TabControl with Datagrid