4

I have built a ViewModelLocator using Unity and have been successfully using it with singleton ViewModel instances. For example:

public class ViewModelLocator
{
    private static readonly UnityContainer Container;

    static ViewModelLocator()
    {
        Container = new UnityContainer();

        if (ViewModelBase.IsInDesignModeStatic)
        {
            //Design Time Data Services
            Container.RegisterType<IMyServiceServiceAgent, DesignMyServiceServiceAgent>();
        }
        else
        {
            //Real Data Services
            Container.RegisterType<IMyServiceServiceAgent, MyServiceServiceAgent>();
        }

        Container.RegisterType<TreeViewViewModel>(new ContainerControlledLifetimeManager());
    }

    public TreeViewModel ViewModel
    {
        get
        {
            return Container.Resolve<TreeViewModel>();
        }
    }
}

The ViewModelLocator is defined as a resource in App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <VMS:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
    </ResourceDictionary>
</Application.Resources>

Which allows me to bind to the ViewModel in any of the Views as follows:

DataContext="{Binding TreeViewModel, Source={StaticResource ViewModelLocator}}" d:DataContext="{d:DesignInstance IsDesignTimeCreatable=False}"

My question is how do I maintain the same pattern (and the blendability) with multiple instances of the same ViewModel?

I have found reference to what I am looking to do in this post How to have multiple pairs "View-ViewModel"? but it does not go into the specifics of implementation.

What I want to be able to do is have multiple instances of these Views/ViewModel pairs for different data trees allowing copy and paste between them etc but cannot think how to cater for specific instances in the ViewModelLocator using the container?

I am assuming I need some kind of collection of ViewModels as per the post mentioned above, but how do I register that collection with the Unity Container and how do I bind to that in the View?

Any help is much appreciated.

Community
  • 1
  • 1
Kristian Wilson
  • 93
  • 1
  • 1
  • 6
  • This is a really good question. I got stuck the same way you did. I know that it has been 4 years but did you solve this problem? Do you have any ideas? I am using MVVM Light. – Stefan Vasiljevic Feb 24 '15 at 15:18

1 Answers1

0

What I did in your situation was to still have a single ViewModel for the view, but have another ViewModel that holds the data that can change.

For instance, if I have a UserView control that displays user information, I have a single UserViewModel bound to that view through the ViewModelLocator. I also have a UserModel class that can change depending on the current user being viewed/edited. This UserModel class inherits from ViewModelBase and is exposed by the UserViewModel class through a property. Somewhere else in the application, if a user is selected for instance, I set the UserViewModel's User property to be the UserModel that I want to be displayed in the UserView.

Matt Casto
  • 2,150
  • 1
  • 22
  • 32
  • Hmm, I can see this working for a single view, but I actually need to have multiple instances of the View/ViewModel with each view tied to a specific VM. In effect I am trying to create a MDI of different trees of information and allow drag drop between them. – Kristian Wilson Jun 28 '11 at 15:02