如何以MVVM友好方式向DataGrid添加新行 [英] How to Add a New Row to DataGrid in MVVM Friendly Way

查看:133
本文介绍了如何以MVVM友好方式向DataGrid添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 DataGrid

<DataGrid CanUserDeleteRows="True" 
          CanUserAddRows="True"
          SelectedItem="{Binding SelectedResource, Mode=TwoWay}"
          ItemsSource="{Binding Path=Resources, Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged, 
                                IsAsync=True}"> ... </<DataGrid>

我正在使用MVVM模式绑定到 ObservableCollection< ResourceViewModel>资源,效果很好。我有一个添加新行的按钮,这是通过向资源集合中添加新的 ResourceViewModel 来完成的-很好。现在,我希望用户能够单击空白的最后一行,这会在 DataGrid 中自动创建一个新记录。

I am using the MVVM pattern to bind to an ObservableCollection<ResourceViewModel> Resources and this works great. I have a button that adds a new row and this is done by adding a new ResourceViewModel to the Resources collection - great. Now, I want the user to be able to click on the empty last row, and this automatically creates a new record in the DataGrid.

我已经确保 DataGrid 具有 CanUserAddRows = True 。我已确保绑定到的集合资源 ResourceViewModel )中的类具有默认构造函数(否参数),并确保集合类型不是只读的。当用户单击最后一行时,默认构造函数将触发,但要正确实例化新的 ResourceViewModel 对象,需要引用 Resources的网格收集...

I have made sure the DataGrid has CanUserAddRows=True. I have made sure the class in the collection Resources (ResourceViewModel) that I am binding to has a default constructor (no parameters) and I have made sure the collection type is not readonly. When the user clicks on the last row the default constructor fires but to correctly instantiate the new ResourceViewModel object in need a reference to either the grid of the Resources collection...

我想我可以在<$上使用 AttachedCommand c $ c> CellBeginEdit 事件,然后将新的 ResourceViewModel 添加到那里的可观察集合中,是否有执行此操作的标准方法?

I suppose I could use and AttachedCommand on the CellBeginEdit event and then add a new ResourceViewModel to the observable collection there, is there a standard way of doing this?

注意,我已阅读以下问题,对我没有帮助

Note, I have read the following questions and these are of no help to me


  1. WPF DataGrid -新行活动?

  2. 如何使用DataGrid和MVVM添加行

  1. WPF DataGrid - Event for New Rows?
  2. How to add rows with a DataGrid and MVVM






编辑。事实证明,由于WPF DataGrid 中的错误,我在执行此操作时遇到了问题。请参阅奈杰尔·斯宾塞的博客。但是,他的修复程序目前不适用于我...


Edit. It turns out that I am having problems doing this due to a bug in the WPF DataGrid. See Nigel Spencer's Blog. However, his fix is not working for me at the moment...

推荐答案

据我了解,您知道如何正确地将新项目添加到您的数据绑定集合中,从而导致将新项目添加到 DataGrid 中,并且您的问题实际上与用户单击以下内容时如何执行此操作有关 DataGrid 的最后一行。在视图模型中处理某些事件的一般方法是创建一个附加属性(如果尚不存在)来为您处理该事件。

As far as I understand, you know how to correctly add new items into your data bound collection to result in a new item being added to the DataGrid and your question actually relates to how can you do this when the user clicks on the last row in the DataGrid. The general way to handle some event in the view model is to create an Attached Property (if one does not already exist) that handles that event for you.

例如,您可以创建一个附加属性,该属性将处理程序附加到相关事件上,并将另一个类型 ICommand ,可以在调用事件处理程序时执行。然后,您可以在视图模型中编写该 ICommand 的功能(在其中向数据绑定集合中添加新项),然后数据绑定 ICommand 实现到附加的ICommand属性

For instance, you could create an Attached Property that attaches a handler to the relevant event and another of type ICommand which you could execute when the event handler is called. Then you could write the functionality of that ICommand in your view model (in which you add a new item to your data bound collection) and data bind your ICommand implementation to the Attached ICommand Property.

我相当确定知道如何定义附加属性 s,我不会向您展示来侮辱您。请让我知道我是否误解了您,或者不清楚。

As I'm fairly sure you know how to define Attached Propertys, I won't insult you by showing you. Please let me know if I have misunderstood you, or not made myself clear.

这篇关于如何以MVVM友好方式向DataGrid添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆