MVVM : Share data between ViewModels

How do I share data between multiple ViewModels ?

For example there is a class named Project in application .

    public class Project : ModelBase
    private string _projectName;

    public string ProjectName
        get { return _projectName; }
            _projectName = value;
            RaisePropertyChanged(() => ProjectName);

In multiple ViewModels application should access ActiveProject. What's the best way to share Project between ViewModels ?

  • Mediator Pattern ? (Messaging)
  • Static object
  • Singleton pattern (If yes how?)

I've used Messaging before but it needs much codding . For all ViewModels I've to create ActiveProject property and also have to register a messenger to update that.

I use MVVM Light framework. Any code example would be appreciated.


I would create a ViewModel that acts as a parent to all the Project ViewModels. (Let's call it Solution)

The Solution ViewModel would have the property ActiveProject and an observable collection of Projects.

Singleton will definitely help. To implement, if I had a class named User:

    private static User mInstance;

    private User () //constructor

    public static User Instance
            if (mInstance == null)
                mInstance = new User();
            return mInstance;

You could have a static collection which your view model populate before you navigate to the new view model. The target view model can then retrieve the data from within it's constructor.

For example ViewModel1 (VM1) will create a Project and populate it. VM1 will then put the Project into a shard, static, collection. VM1 will then navigate to another view model (VM2). In the constructor of VM2 you would go to the collection and retrieve the Project placed in there by VM1.

If you used a dictionary of key-value pairs it would also allow you to share other data between view models.

I would recommend the Mediator Pattern. I have used an EventAggregator for this type of messaging between VM's before and there is really not much to it.

Don't, don't. Don't use singletons this way in your MVVM application. In fact, the Project class should be a model for your ViewModels. Just pass it in vm's constructor. If you really need to share one instance of Project class in multiple vm's, then use factories and some type of cache when constructing view models. If your vm reguires some more information, just create special Model class which will derive from Project (or implement IProject), so you can easilly use interface segregation principle.

Need Your Help

General purpose physics engine

scientific-computing physics-engine

Is there any general purpose physics engine that allows huge simulations of rigid bodies? I'm using PhysX from Nvidia, but the focus of this engine is game development, soft bodies. I want to know if

Core Data Migration - “Can't add source store” error

mapping-model core-data-migration

In my iPhone app I'm using Core Data and I've made changes to my data model that cannot be automatically migrated over (i.e. added new relationships). I added the data model version (Design -> Data...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.