Anirban Paul
Anirban Paul

Reputation: 1165

how to perform communication between container ViewModel and and contained ViewModel?

Please consider the following code

    class VMContainer:INotifyPropertyChanged
    {
    Type t;
    VMContained contained;

    public Type T
    {
        get
          {
               return this.t;
          }
        set
          {
               this.t = value;
               this.OnPropertyChanged("T");
          }
    }
    ........
    ........
    ........

    }

VMContainer and VMContained are two ViewModel classes. Now I need to change one property(say, P1) of member instance "contained" whenever the Container class property T changes.How should I do that? Please advise.

Upvotes: 0

Views: 71

Answers (2)

Andrew Cooper
Andrew Cooper

Reputation: 32586

Simplest way would just be to set the value in the setter for T:

public Type T
{
    get
      {
           return this.t;
      }
    set
      {
           this.t = value;
           contained.P1 = CalculateContainedValue();
           this.onPropertyChanged("T");
      }
}

Update

public Type T1
{
    get
      {
           return this.t1;
      }
    set
      {
           this.t1 = value;
           contained.P1 = CalculateContainedValue();
           this.OnPropertyChanged("T1");
      }
}

public Type T2
{
    get
      {
           return this.t2;
      }
    set
      {
           this.t2 = value;
           contained.P1 = CalculateContainedValue();
           this.OnPropertyChanged("T2");
      }
}

private Type CalculateContainedValue()
{
    return /* Some combination of T1, T2, ... */ ;
}

Upvotes: 3

D J
D J

Reputation: 7028

You can use EventAggregator. Sample is here.

Else you have to make your own pub sub mechanism.

Upvotes: 0

Related Questions