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 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
.
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...
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
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...