Harold Finch
Harold Finch

Reputation: 586

How to access to all viewmodels via the viewmodel base?

I've created a ViewModel class that have inside the implementation of INotifyPropertyChanged, now I also have other ViewModels that inherit from the ViewModel (base). All working good actually but I have a doubt. So let's say that I've in the CustomerViewModel an ObservableCollection called Price, like this:

private ObservableCollection<Models.Price> _price = new ObservableCollection<Models.Price>();
public ObservableCollection<Models.Price> Price
{
     get { return _price; }
}

this ObservableCollection should be populated by other classes, 'cause I need to access to the same resource.

I really don't understand how can I do this in mvvm. I though to a Singleton ViewModel, so something like this defined in the base VM:

 public static ViewModel Instance { get; set; }

So import all the childs VM into the base and access them via ViewModel.Instance.Price;

but doesn't seems a good practice for me. Any idea?

Upvotes: 1

Views: 268

Answers (2)

Jai
Jai

Reputation: 8363

There are generally two approaches to this.

  1. Even if you don't have a real database/repository, implement a singleton class that simulates this. This singleton class should also implement INotifyPropertyChanged (and/or INotifyCollectionChanged as appropriate). All ViewModels will be able to access this simulated repository, and interested ViewModels can choose to subscribe to this repository's PropertyChanged callback. For your question, it is generally tidier to have a repository that handles just prices, rather than having a simulated repository that stores 101 different information.
  2. Have a main ViewModel. Some people would visualize that the MainWindow being the main view, with a corresponding main ViewModel. This ViewModel is intentionally made a singleton, which other ViewModels can access through static call. This main ViewModel is basically acting just like #1 - it is like a repository that stores 101 different information. That main ViewModel is likely to look untidy, but it is simply to trace where things are stored - if you need any data, it's probably in there.

Personally, I prefer to use the first approach. lokusking's answer is an example of this approach. Although his "repository" does more than storing data.

Upvotes: 0

lokusking
lokusking

Reputation: 7456

With this implementation, you can share the same Datasource to all ViewModels

public class PriceGenerator {

        private PriceGenerator() {
            this.Prices = new ObservableCollection<Price>();
            this.Generate();

        }

        void Generate() {
            //Generate Objects here

            this.Prices.Add(generatedPrice);
        }

        public ObservableCollection<Price> Prices {
            get;
        }


        private static PriceGenerator _instance;
        public static PriceGenerator Instance => _instance ?? (_instance = new PriceGenerator());

    }

Upvotes: 1

Related Questions