Example of WPF Editable DataGrid and Catel MVVM

Jun 2, 2012 at 10:57 PM

Does anyone have an example of how to use Catel MVVM in a WPF Editable DataGrid?

I have bound a WPF DataGrid to an ObservableCollection of ViewModels (each representing a row in the grid).

Amongst other questions there are: at what point, where and how should I save the data if a row has changed? Should this be in the ViewModels

protected override void OnEndEdit(System.ComponentModel.EditEventArgs e)

method? And if so, is there a method to see if a row has changed when reaching the OnEndEdit method?

I though the property HasDirtyModel of the ViewModel could be used, but this always seems to be false in the OnEndEdit method whether something has been edited in the grid row or not.

And the IsDirty property of the Model always seems to be true even if no fields have been modified in the grid row at all.

Some help, guidance, examples would be wonderful!



Jun 3, 2012 at 7:58 AM

I don't think it is a good idea to bind every row to a separate view model for performance reasons. In such a case, you should directly bind the models (because grids can contain thousands of rows, you really don't want to spend time creating a view model, or it must be that every row has lots of logic. But in that case, don't use a grid.

A grid is meant for direct data editing.

Jun 3, 2012 at 10:56 AM
Edited Jun 3, 2012 at 10:57 AM

If a grid contains thousands of rows, you will have performance issues anyway :)  Besides that it's all a matter of opinion. Some developers say use a view model others say don't. And some others even say don't use MVVM at all :D It's the same discussion as with Service Locators. Some find it haven on earth and now more and more say it's not good for maintenance to put everything in a service and use a Service Locator (because when something goes wrong it's hard to find whether you are 'talking' to the service you want instead of some other (test)service).

Besides from this discussion, the question stays the same. If I bind to a model, I still have to know where and how to do the saving (and control logic).

'My' grid contains about max ten to fifteen rows for a TimeLog (of one week). You can select projects in a row, you can delete a row (but not always), you have to check hour entries, you have to totalise hours etc. This seems very much as view model stuff to me...

Jun 3, 2012 at 1:36 PM

I always use a single save button that will save the main view model that contains the models. This way, you save the changes for multiple sets at once.

Jun 3, 2012 at 1:51 PM
Edited Jun 3, 2012 at 1:56 PM

A good solution, but 'my' users want to save a row when they hit the Enter button in a grid line (after entering their hours for that task). I guess I'll have to do it in the EndEdit method.

What I don't understand is why HasDirtyModel is always false and IsDirty always true?

Does Catel keep a backup of the model in a ViewModel (using the Memento pattern which has an implementation in Catel)? Or do I have to do that myself?

And if it does, how can I check if a model has changed?

Jun 3, 2012 at 2:30 PM

Strange. Do you have a repro for the IsDirty stuff? The view model itself is dirty, but the models are not. This is only possible if properties on the vm are not mapped to models but have changed.