0

I am working with MVVM application using MVVM light. Here I have 2 Views and related ViewModels like Header and Orders

 public ObservableCollection<HeaderViewModel> HeaderControls
    {
        get { return _header ?? (_header = new ObservableCollection<HeaderViewModel>()); }
    }

    public ObservableCollection<object> ViewControls
    {
        get { return _viewControls ?? (_viewControls = new ObservableCollection<object>()); }
    }

I am loading these ViewModels in observable collection to load related views.

  private void LoadControls()
    {
        this.HeaderControls.Clear();
        var headerViewModel = new HeaderViewModel();
        this.HeaderControls.Add(headerViewModel);

        this.ViewControls.Clear();
        var orderViewModel = new OrdersViewModel();
        this.ViewControls.Add(orderViewModel);
    }

Now OrderViewModels have few properties like text fields, grid, those I want to update via click of icons on HeaderViewModel.

I have also tried the solution posted on, but it does not helped : Accessing Properties in other ViewModels in MVVM Light

Community
  • 1
  • 1
Milind Chavan
  • 115
  • 1
  • 10

1 Answers1

0

Communication in MVVM Light between Model<->VM<->View's are normally via the Messenger class.

In short, the Messenger allows you to send a "message"(could be a string, int, pretty much any type you want) from one class to another. The receiver subscribes to receive these message(either based on type of message it wants to get or via a combination of who sends it and the type). Similarly the sender can also just send a message to a specific other class or to anyone who is subscribed to the type of the message.

One of the main advantage's of the Messenger is it's based on weak dependency hence you don't have to create a strong dependency between something like VM<->View, thereby staying true to MVVM principles. It's also well documented and you should be able to get ample help in "how to use it" from links such as:

MVVM Light - what's the Messenger?

MVVM Light Toolkit Messenger V2

or even this answer

as I've mentioned in that answer, one of the concepts shown by the download example in it is "Messenger class usage with a custom message type OpenWindowMessage". You can see how the message being sent is a custom type and how VM's subscribe/send and act on these messages.

Side-note:

From the code you've posted it's hard to say this for sure but it seems pretty weird you creating a ObservableCollection<object> ViewControls. I see you're adding a OrdersViewModel to it, so why aint the type just OrdersViewModel for the collection or even ViewModelBase. All your VM's should be inheriting from ViewModelBase anyways.

Community
  • 1
  • 1
Viv
  • 17,170
  • 4
  • 51
  • 71