MoonKnight MoonKnight - 1 year ago 250
C# Question

How to Add a New Row to DataGrid in MVVM Friendly Way

I have the following


<DataGrid CanUserDeleteRows="True"
SelectedItem="{Binding SelectedResource, Mode=TwoWay}"
ItemsSource="{Binding Path=Resources, Mode=TwoWay,
IsAsync=True}"> ... </<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
to the
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

I have made sure the
. I have made sure the class in the collection
) 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
object in need a reference to either the grid of the

I suppose I could use and
on the
event and then add a new
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 - Event for New Rows?

  2. How to add rows with a DataGrid and MVVM

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

Answer Source

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.

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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download