将 WPF4 Datagrid 绑定到空集合
我有一个对象集合,我试图将其绑定到 DataGrid。当集合中有很多对象时,它工作得很好。但是,当集合为空时,我在 DataGrid 中看到一个空白行,但无法编辑它。当我将其绑定到具有空列表的集合时,会出现两行,并且可以用第一行空来编辑第二行,即使我有验证。我尝试过普通的 List<> 和 ObservableCollection<> 。
如何才能使 DataGrid 可以输入空列表,或者 DataGrid 不允许用户输入第二个空白行?
I have a collection of objects which I am trying to bind to a DataGrid. When the collection has a lot of objects in it, it works fine. When the collection is empty, however, I see a blank row in the DataGrid, however, I cannot edit it. When I bind it to a collection which has an empty list, then two rows appear, and it is possible to edit the second row with the first empty, even though I have validation. I have tried both an ordinary List<>, and an ObservableCollection<>.
How can I make it so that either the DataGrid can enter into an empty list, or that the DataGrid won't let the user enter into the second blank row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不管它的价值如何,我确实弄清楚了我的问题是什么。好吧,我修复了这个故障,但我完全不确定为什么它会起作用。
在 DataGrid 的 Binding 字段中,我打开了 Mode=TwoWay。我认为这是必要的,因为我希望网格返回到数据,反之亦然。事实证明,删除它就可以解决问题。
For what its worth, I did figure out what my problem was. Well, I fixed the glitch, but I am not at all sure why it worked.
In the Binding field of the DataGrid, I had Mode=TwoWay on. I thought this was needed, as I wanted the grid to go back to the data, and vice versa. Turns out, removing this fixed the problem.