MVVM update of calculated properties

前端 未结 5 671
粉色の甜心
粉色の甜心 2020-12-17 21:54

I\'m just learning MVVM, and I\'m trying to work how to display changes to a calculated property as a result of changes to the values from which it\'s calculated. All of the

5条回答
  •  心在旅途
    2020-12-17 22:37

    The solution most often offered here seems to be to get all of the properties on which the tax value depends to invoke PropertyChanged in the ModelView for the property itself and for every property that depends on it.

    No the supporting properties do not need their own change notification unless they are being displayed. But each property will need to call the tax value's OnPropertyChanged("TaxValue") in their setter(s) either directly; or indirectly as per the example below. That way the UI gets updated because a supporting property has changed.

    With that said, let us consider an example. One way is to create a method which will do the value calculation. When the ultimate value is set (TaxValue below) it will call OnNotifyPropertyChange. That operation will inform the user of the TaxValue change to the whole world; regardless of what value triggers it (Deduction|Rate|Income):

    public class MainpageVM : INotifyPropertyChanged 
    {
           public decimal TaxValue 
            {
               get { return _taxValue; }
               set { _taxValue = value; OnPropertyChanged(); }  // Using .Net 4.5 caller member method.
            }
    
            public decimal Deduction
            {
               get { return _deduction; }
               set { _deduction = value; FigureTax(); }
            }
    
            public decimal Rate
            {
               get { return _rate; }
               set { _rate = value; FigureTax(); }
            }
    
            public decimal Income
            {
               get { return _income; }
               set { _income = value; FigureTax(); }
            }
    
            // Something has changed figure the tax and update the user.
            private void FigureTax()
            {
               TaxValue = (Income - Deduction) * Rate;
            }
    
    
        #region INotifyPropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;
    
            /// Raises the PropertyChanged event.
            /// The name of the property that has changed, only needed
            /// if called from a different source.
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
        #endif
        }
    

    Edit

    To use CallerMemberName (and other items) in .Net 4 install the Nuget package:

    Microsoft.BCL.

    or if not use the standard OnPropetyChanged("TaxValue") instead.

提交回复
热议问题