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'm using the MVVM pattern to bind to ObservableCollection<ResourceViewModel> Resources , and it works fine. I have a button that adds a new row, and this is done by adding a new ResourceViewModel collection to the Resources collection - fine. Now I want the user to be able to click on the empty last row, and this will automatically create a new record in the DataGrid .
I made sure the DataGrid has CanUserAddRows=True . I made sure that the class in the Resources collection ( ResourceViewModel ) that I am attaching to has a default constructor (no parameters), and I made sure that the type of the collection is not readonly. When the user clicks on the last line, the default constructor starts, but to correctly create a new ResourceViewModel object, a link to the grid of the Resources collection is required ...
I suppose I could use AttachedCommand in the CellBeginEdit event and then add a new ResourceViewModel to the observable collection there, is there a standard way to do this?
Notice I read the following questions and they do not help me
Change It turns out I'm having problems with this due to an error in the WPF DataGrid . See Nigel Spencer's blog . However, fixing it now does not work for me ...
source share