I have several properties that return values dependent on another property's value. What is the best way to update the UI bound to a dependent property? See example below, how would you update a UI element bound to the TotalQuantity property of the Parent object when a Quantity in the Children collection changes?
public class Child : INotifyPropertyChanged
{
    private int quantity;
    public int Quantity
    {
        get
        {
            return quantity;
        }
        set
        {
            quantity = value;
            OnPropertyChanged("Quantity");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);
        if (this.PropertyChanged != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}
public class ParentObject : INotifyPropertyChanged
{
    private ObservableCollection<Child> children;
    public ObservableCollection<Child> Children
    {
        get
        {
            return children;
        }
        set
        {
            children = value;
            OnPropertyChanged("Children");
        }
    }
    public int TotalQuantity
    {
        get
        {
            return Children.Sum(c => c.Quantity);
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);
        if (this.PropertyChanged != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}
				
                        
When you instantiate your
ObservableCollection, you need to subscribe to theCollectionChangedevent.This will be called when an item is added/removed from the collection. You simply need to notify that
TotalQuantityhas changed.In the case where you need to update the
TotalQuantityproperty on the UI whenever a child changes, then you simply need to subscribe to the child'sPropertyChangedevent.So, when you add an item to the collection, subscribe to the event:
And in your event handler, you can test to see what property has changed.
Or, you could just call
OnPropertyChangedwithout checking if you're a badass. But I wouldn't recommend it just in case your model gains more properties in future.Don't forget to unsubscribe the event before you remove the child from the collection.