How to Add a New Row to DataGrid in MVVM Friendly Way
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 Property
s, I won't insult you by showing you. Please let me know if I have misunderstood you, or not made myself clear.
MoonKnight
Updated on June 25, 2022Comments
-
MoonKnight almost 2 years
I have the following
DataGrid
<DataGrid CanUserDeleteRows="True" CanUserAddRows="True" SelectedItem="{Binding SelectedResource, Mode=TwoWay}" ItemsSource="{Binding Path=Resources, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 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 newResourceViewModel
to theResources
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 theDataGrid
.I have made sure the
DataGrid
hasCanUserAddRows=True
. I have made sure the class in the collectionResources
(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 newResourceViewModel
object in need a reference to either the grid of theResources
collection...I suppose I could use and
AttachedCommand
on theCellBeginEdit
event and then add a newResourceViewModel
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
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...