[MVVM] ViewModelManager

Topics: Feature requests
Jul 20, 2012 at 2:17 PM
Edited Jul 20, 2012 at 2:17 PM

Depending to http://catel.codeplex.com/workitem/7120

"If someone can provide a use case, we might reconsider it."

I have a use case. That was the reason why i open the Issue.

I have some viewmodels and some corresponding models.

And I have some views. And always two views belong to one model and one viewmodel.
When the view is generated the datacontext is the model. So i wan't the corrosponding viewmodel.

For Example:

View5A is generated. The datacontext is Model5. So when there is no ViewModel5 I generate it.

View5B is generated. The datacontext is Model5. So i want to search for the ViewModel which belongs to Model5.

Jul 23, 2012 at 6:32 PM

ok, issue re-opened.

Aug 3, 2012 at 1:27 PM

Feature implemented.

1) Get the view model manager via the service locator:

var vmManager = ServiceLocator.Instance.ResolveType<IViewModelManager>();

2) Get the view models that contain a model:

var viewModels = vmManager.GetViewModelsOfModel(myModel);


It is now also possible to retrieve the views of a view model using the IViewManager.

Aug 21, 2012 at 7:21 AM

Ok. I don't understand everything in this solution

whats the realation between model and viewmodels. n:n? / n:1? / 1:n?

1. Get the view models that contain a model  GetViewModelsOfModel ( models 1 to viewmodels n)

2. UnregisterAllModels ( models n to viewmodels 1)

Aug 21, 2012 at 9:35 AM

In theory, it is possible that the relation between model and view model is n:n. A view model can have multiple models, and a model can be used in multiple models. However, we recommend to use a 1:n relation (1 view model, which can use several models).

That's why you can get an enumeration of view models for a model, but in most cases, this will be just one.

Aug 21, 2012 at 9:57 AM

Hm. Ok.

I miss a method to delete all relations. For Example UnregisterAll. Is this possible?

Aug 21, 2012 at 10:05 AM

You should not be able to delete all relations. The view model takes care of this, why would you want to do such a thing?

Aug 21, 2012 at 10:25 AM

In my application I load a profile. This profie generates all models. So when i change the profile all models deleted and new models created. So I also have to delete all relations and create new ones.

Aug 21, 2012 at 10:54 AM

This should not be done via the IViewModelManager. You should, for example, use the MessageMediator to broadcast a full UI update and re-create the view models. For example, what should happen to models in an edit state, etc? I think what you are trying is more complex than it looks at first sight.

Aug 21, 2012 at 12:16 PM
Edited Aug 21, 2012 at 12:24 PM

Hello. Thanks for your answer.

At the moment its not clear for me what advatages I have with a MessageMediator, or why I need a MessageMediator at all.

And why should there happen something when the models are in edit state. 

I thought it's easy. Have a model and a view. Show the view give the model as datacontext and the viewmodel is created. So when the model is deleted there is no more need to show the view and also no need for the viewmodel and the realation. So delete everything.