0
votes

I'm trying to edit a Master-Detail View multiple times in Catel.

My question is, if there is no common way in catel to handle a Master-Detail-View and multiple editions using the SaveViewModelAsync and CancelViewModelAsync methods?

The workflow works when my Master-ListItem is no UserControl with it's own ViewModel and when I change the selected Master-ListItem after save or cancel, so that a new ViewModel will be created. But I don't want to null or change the selection after a save/cancel. Also I have maybe to create a UserControl + ViewModel for the Master-ListItems.

Restrictions of the question:

I have got the information from Geert van Horrik from the discussion of his answer, that

  • multiple edit cycles are not supported directly
  • one Model should only be accesd by one ViewModel

Problem:

I can use the the SaveViewModelAsync and CancelViewModelAsync only once. After that, the ViewModels which are alive

  • will get no updates from the Model
  • don't create a BackUp from the Data, so that the changes can be reverted again

Code to Reproduce:

I have created a WPF Project, where I tested this all.

Possible solution:

  • I could use the EditableObjectHelper and handle this by myself
  • I have to instantiate the VewModel again, after the save or cancel, like it happends on a selection change in my Example Code

Restricion of the solution:

  • If I use a specialized UserControl for the ListItems of the Master-View, 2 ViewModels are looking on 1 Model. After the SaveViewModelAsync the ViewModel doesn't get any Notifications of the Model. So I would have to instantiate this ViewModel again too. But I would Breake the 1-1 relation of the ViewModel-Model.

My conclusion:

It seems, that I have to instantiate all these ViweModels and handle this workflow all by myself. It seems, that the ViewModelLifetimeManagement.PartlyManual (CloseViewModelOnUnloaded is Obsolete) doesn't work here. On selection change, always a new ViewModel will be created, the old one will probably just not be closed.

I don't want to misuse the framework. Maybe I have missed something. I hope somebody can help me or give me a tip, how to handle this.

2

2 Answers

1
votes

Catel calls Save / Cancel as soon as the view model get's unloaded. If you want to do "intermediate" saves without changing the data model, you can:

  1. Save the master list which will save the dirty models (you can directly modify the models from within your vm's)
  2. Create a custom command (SaveData) that you can run from an explicit button (or input gesture binding) to save the data without calling SaveAsync on the vm
0
votes

After the answer of Geert van Horrik, I currently ended up , don't using the SaveViewModelAsync and CancelViewModelAsync methods and handle the save and cancel by my own, so that the notification will not breake. I'm using the GetChildViewModels() method on the ViewModelBase and the EditableObjectHelper.CancelEditObject(Model);