CloseViewModelOnUnloaded

Topics: Issues / bugs
Feb 6, 2013 at 9:07 AM
Hello

Have a problem with CloseViewModelOnUnloaded. Don't want to close the viewmodel. Have a ObservableCollection of Models. The current Model is the datacontext of a UserControl. The view genarates the corresponding viewmodel.

When I change the current model a new view and the viewmodel is generated. Thats ok. But when I change back to the previous model the viewmodel is generated again.

CloseViewModelOnUnloaded is set in the view to false.

What's wrong?
Coordinator
Feb 7, 2013 at 1:41 PM
I have no idea. Can you provide a repro?
Feb 8, 2013 at 1:09 PM
Issue and Repro created:

http://catel.codeplex.com/workitem/7244
Coordinator
Feb 9, 2013 at 1:35 PM
This is because you use the same view again for a different datacontext. Then it is impossible to keep a history of view models used by the view.

The CloseViewModelOnUnloaded is especially meant for tab controls that are unloaded (but you don't want to loose the VM data for the hidden tab). This is something completely different because you are re-using the same view for a different context.

We have a feature in Catel that can accomplish what you need, but it requires some custom work.
  1. Create a new ViewModelFactory class deriving from Catel.MVVM.ViewModelFactory
  2. Override the CreateViewModel method. If the view model type AND the dataContext are the same, you can return the same instance of the VM. You can then keep track of all view models inside the view model factory.
Jun 28, 2013 at 8:03 AM
Can I ask one more question to this discussion. Its not realy clear for me.

You write CloseViewModelOnUnloaded is only for tabcontrol. But only for tabcontrol extended. Not with normal tabcontrol.

It also don't works with combobox, when you choose an other value in an combobox a new viewmodel is generated. But with listview it works. When you choose an other value in an listview it don't loose the viewmodel.

Example: The following ist an listview with binding to an observable Collection with model. The items are defined as catel:usercontrol in datatemplate.
  • select value1 and the viewmodel vmv1 is generated.
  • select value2 and vmv2 is generated.
  • select value1 again and vmv1 is used again

|Value1|

Value2
Value3
Value4


Try this with an combobox then always when you select an other value the old viemodel ist closed.