ViewModel Binding With Multiple Instances of View

Mar 17, 2010 at 5:25 PM
What is the recommended step when you have multiple instances of a view and need a separate DataContext for each? One option I see is to declare multiple instances of the view model in ViewModelLocator though it seems like this can get messy with many instances (I'll have 12). The other option is to set the DataContexts in code but I feel like this could quickly lead to high coupling. How have you handled this in your applications?
Mar 22, 2010 at 10:24 PM


Sorry for the delay, last eek was a bit crazy at MIX ;)

The way I handle this is by making the ViewModel property in the ViewModelLocator (for example the Main property for the MainViewModel) return a different instance of the MainViewModel each time it is called. This way each view will have a separate instance of the MainViewModel.

However, this means that resolving the DataContext of a given window is a bit more complex (since the ViewModel is not kept static anymore). If you need to resolve, you can build a lookup table in the ViewModelLocator based on a unique ID stored in the View for example, or use an IOC container to instantiate and resolve the ViewModel instances according to a key.

Hopefully this helps,



Mar 30, 2010 at 3:25 PM

Thanks for the response.  Thats a good approach which I didn't think of.  In my case I know exactly how many instances of each View I'll need so I create multiple static ViewModels in the locator.

If we run into this again and don't know how many instances we'll need, I'll definitely use your approach.  Thanks!

Jun 6, 2012 at 11:41 AM

Hi Laurent,

I'm currently facing the same issue. Do you have a piece of code showing how to do that ? I know to generate an ID, I know how to inject it in the view, but I don't know how to send this unique ID to the locator. I don't see how to pass an unique ID from the view to the viewmodel locator in order to return the right instance of the view model. Thanks a lot for your help and your great Framework !


Jun 6, 2012 at 11:43 AM

Sorry for asking a technical question here, I'll post it on Stackoverflow ;)