Search code examples
c#mvvmwindows-phone-8inotifypropertychangedfody-propertychanged

Property depending on property from another class


I've got a Windows Phone 8 application using Fody to inject INotifyPropertyChanged into properties. I have Class First with property A which is binded to textbox in View:

[ImplementPropertyChanged]
public class First
{
    public int A { get; set; }

    public int AA { get {return A + 1; } }
}

And class Second with property B depending on property A (also binded to textbox):

[ImplementPropertyChanged]
public class Second
{
    private First first;

    public int B { get {return first.A + 1; } }
}

Updating A and AA works fine, however B doesn't update automatically when first.A changes. Is there an easy and clean way to achieve such automatical updates using fody or do I have to create my own event to handle it?


Solution

  • I ended up using standard INotifyPropertyChanged in the way SKall suggested.

    public class First : INotifyPropertyChanged
    {
        public int A { get; set; }
    
        public int AA { get {return A + 1; } }
    
        (...) // INotifyPropertyChanged implementation
    }
    
    public class Second : INotifyPropertyChanged
    {
        private First first;
    
        public Second(First first)
        {
            this.first = first;
            this.first.PropertyChanged += (s,e) => { FirstPropertyChanged(e.PropertyName);
    
            public int B { get {return first.A + 1; } }
    
            protected virtual void FirstPropertyChanged(string propertyName)
            {
                if (propertyName == "A")
                    NotifyPropertyChanged("B");
            }
    
            (...) // INotifyPropertyChanged implementation
        }
    };