I ve been looking into MVVM lately, and after I discovered Caliburn.Micro things have been pretty swell; I m still in the early learning stages, but I belive I have an OK feel for the MVVM basics.
I m bumping into issues with WPF DataGrid
, though - and it s pretty much the same issues that I had with WinForms DataGridView
: how the heck do you handle CanUserAddRows=true
in-grid item adding cleanly?
I obviously don t want to add DataGrid
-specific hacks to my ViewModel, since it ideally should be repurposable for other View
controls. At the same time, I d like to be able to get a notification when a new row item has been added, so I can persist it right away.
I m binding the DataGrid
to a BindableCollection<FooModel> FooItems
- with a clean MVVM design, if I understand things correctly, I would be able to handle FooItems.CollectionChanged
and react to Add/Remove events. However, the DataGrid
fires the Add event as soon as a default-constructed item is added - this is obviously not the right time to persist the object!
After a lot of googling and digging through StackOverflow, I m getting the impression that DataGrid
is being utterly retarded in how it s firing the Add/Remove events. People who use it with CanUserAddRows=true
seem to only work on in-memory collections, where people who persist data seem to use separate input fields + buttons Commands to add new items.
My FooModel
implements INotifyPropertyChanged
but not IEditableObject
- as far as I can tell that shouldn t be the problem, though, since IEO seems related to property edit/undo, whereas my problem is with when the Add event is fired...
So, what do you do to handle in-grid editing cleanly?