I'm using MVVM Light for a WPF application with tabs (a bit like Internet Explorer). Each tabs has to be a couple of unique View and View models. I know know how many instances I will have in my tab control.
Due to the design of MVVM Light I can't have multiple instances of a ViewModel at the same moment (static property in the locator). On the codeplex Website i found the following solution :
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.
I understand I have to inject an ID in each view, I understand I have to maintain a list of these IDs in the Locator, I understand I have to lookup in the locator to associate each view to the right locator, but I have no idea how to pass this ID from my view to the Locator. How can I do that in the XAML of my view ?
I hope my question is clear enough, let me know if you need more details.