Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
        set
        {
            _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.

like image 431
Unforgiven Avatar asked May 13 '13 20:05

Unforgiven


1 Answers

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.

like image 118
Emond Avatar answered Oct 01 '22 15:10

Emond