Dynamically creating ViewModels with MVVM light toolkit
I have a search view and details(2-3) views that depend on the selection of the search view. Up on selection the search view model sends the message to change the view to Main viewModel and also sends a message with the selected ID.
When the main view model receives the view change message, it will call the detail view 1. this will look for the view model specified in datacontext in Locator. when this view details are verified, detail view 2 should be displayed with the details of selected user in search.
I was watching the MIX 11 videos. Got some idea. But I want to do this using MVVM toolkit 3.
So if I create a class with all the detail view models and populated the required viewmodel objects on selection changed event, I should be good. But should I开发者_StackOverflow still declare the static VM's in Locator? how do I communicate to the View model locator that, if a view is looking for data context of type detailviewmodel1, use this class instead of the static VM in the Locator?
I'm not sure I see the need for dynamic ViewModel. A ViewModel is only a wrapper around one or more models to provide hooks for the View. With that said, if you already know the View, you already know the ViewModel and should implemented it at design time.
Reading over your post it looks more like you are looking to have multiple ViewModel's for one View. If this is correct you are going to need to look into dependency injection. This will allow you to inject VM's into Views based on certain criteria. My personal recommendation for this would be ninject.
Perhaps someone with more MVVM Light experience can come along and give more info. I am a Caliburn.Micro fella myself and although a bit more complex than MVVM Light it lends itself to a bit more flexibility in terms of constructor injection and other such things.
精彩评论