Question

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?

Was it helpful?

Solution 2

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
    }
};

OTHER TIPS

I am not familiar with Fody but I suspect it is because there is no setter on Second.B. Second should subscribe to changes in First and if First.A is the property being changed then that should use the (private) setter of B.

Alternatively subscribe to First and then invoke B property changed event:

[ImplementPropertyChanged]
public class Second
{
    private First first;

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

    public Second(First first)
    {
        this.first = first;
        this.first.OnPropertyChanged += (s,e) =>
        {
            if (e.PropertyName == "A") this.OnPropertyChanged("B");
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top