Ani
Ani

Reputation: 3

Updating another property based on a property, vice versa

My issue is I have three properties with mutually dependent in my WPF application. I have implemented InotifyPropertyChanged also for the class. I am not sure how to update second property based on other.

Example:

Properties : ActualValue, ChangedValue, Change

ActualValue is Fixed, and it is possible to update ChangedValue and Change. That means if I update ChangedValue, the Change is calculated like (ActualValue-ChangedValue) and set. And when Change updates, the ChangedValue will be calculated based on ActualValue and Change.

Upvotes: 0

Views: 945

Answers (1)

McGarnagle
McGarnagle

Reputation: 102723

You can put the logic inside the setters of properties that others depend on. Since it's a circular dependency, just make sure that inside the setters you change the private variable-- don't use the property setter, as that would create an infinite loop. Like this:

private string _change, _changedValue;

public string ChangedValue {
    set { 
        _changedValue = value;
        _change = ActualValue - _changedValue;
        NotifyPropertyChanged("ChangedValue");
        NotifyPropertyChanged("Change");
    }
}


public string Change {
    set { 
        _change = value;
        _changedValue = ActualValue - _change;
        NotifyPropertyChanged("Change");
        NotifyPropertyChanged("ChangedValue");
    }
}

Upvotes: 1

Related Questions