'WPF: Property changed on List<Object>, will that affect its own class of another property..?

When I change the value of any DenomList item, how to affect the Total value?

This I tried and modifying the list item is not affecting the TotalValue property.

    int _totalValue;
    public int TotalValue
    {
        get { return _totalValue;}
        set { 
              _totalValue = value; 
              RaisePropertyChanged("TotalValue"); 
            }
    }

    List<MarkDenom> _denomList;
    public List<MarkDenom> DenomList
    {
        get { return _denomList; }
        set
        {
            this._denomList = value;
            TotalValue = _denomList.Select(o => o.Total).Sum();
            RaisePropertyChanged("DenomList");
        }
    }


Solution 1:[1]

    int? _totalValue;
    public int TotalValue
    {
        get 
        { 
           return _totalValue ??= _denomList.Select(o => o.Total).Sum();
        }
    }

    List<MarkDenom> _denomList;
    public List<MarkDenom> DenomList
    {
        get { return _denomList; }
        set
        {
            this._denomList = value;
            _totalValue = null;
            RaisePropertyChanged("DenomList");
        }
    }

From what I've got from your code, the TotalValue property shouldn't be changed other than in case if DenomList value is changed. So here is upgraded code that allows calculating TotalValue on demand instead of on each change of DenomList. You may also want to force TotalValue recalculation after the list content changes, to do so just remove the backing field int? _totalValue completely, leaving just the function behind the property: public int TotalValue => _denomList.Select(o => o.Total).Sum();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xander