locked
Where should I Subscribe to Calculate Total Property in my ViewModel RRS feed

  • Question

  • Hello,

    I have two view models (PlanningAreaDimensionDetailsViewModel and PlanningTemplateViewModel). Please see code below...

    In the PlanningTemplateViewModel I would like to have total property which will reflect changes in properties month_01 to month_12. So after any of those values is changed total is recalculated. I would appreciate a lot if someone can help me and tell me (adjust my code below) where to subscribe in order to reflect changes in properties month_01 to 12 and recalculate Total.

    Currently I dont know how to make it works...Thanks a lot Almir

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using RBBH.Application;
    using RBBH.Model;
    using System.Collections.ObjectModel;
    using GalaSoft.MvvmLight.Command;
    using System.Windows.Input;
    using System.Collections.Specialized;
    using System.Windows.Data;
    using System.Globalization;
    using System.Windows;
    
    namespace RBBH.ViewModels.PlanningViewModel
    {
        public class PlanningAreaDimensionDetailsViewModel : WorkspaceViewModel
        {
            private readonly IUIDataProvider _dataProvider;
            private ObservableCollection<PlanningTemplate> _planningTemplateViewModel { get; set; }
            private IToolManager _toolManager;
            
              
            public PlanningAreaDimensionDetailsViewModel(IUIDataProvider dataprovider, string selectedPlanningArea,
                 IToolManager toolManager = null)
                : base(toolManager)
            {
    
                _dataProvider = dataprovider;
                _toolManager = toolManager;
    
                
            }
    
            /// <summary>
            /// Returns a collection of all the PlanningTemplateViewModel objects.
            /// </summary>
            public ObservableCollection<PlanningTemplateViewModel> PlanningTemplate { get; private set; }
    
            public void CreateTemplate()
            {
                List<PlanningTemplateViewModel> all = (from pt in GetPlanningTemplate() select new PlanningTemplateViewModel(pt, _dataProvider, _toolManager)).ToList();
    
                foreach (PlanningTemplateViewModel ptVM in all)
                    ptVM.PropertyChanged += this.OnViewModelPropertyChanged;
    
    
                    this.PlanningTemplate = new ObservableCollection<PlanningTemplateViewModel>(all);
                    this.PlanningTemplate.CollectionChanged += this.OnCollectionChanged;
    
            }
    
    
            #region  Base Class Overrides
    
            protected override void OnDispose()
            {
                foreach (PlanningTemplateViewModel ptVM in this.PlanningTemplate)
                    ptVM.Dispose();
    
                this.PlanningTemplate.Clear();
                this.PlanningTemplate.CollectionChanged -= this.OnCollectionChanged;
    
            }
    
            void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
             
                // When a customer is selected or unselected, we must let the
                // world know that the TotalSelectedSales property has changed,
                // so that it will be queried again for a new value.
                if (e.PropertyName == "Month_01" || e.PropertyName == "Month_02" || e.PropertyName == "Month_03" || e.PropertyName == "Month_04" || e.PropertyName == "Month_05" ||
                    e.PropertyName == "Month_06" || e.PropertyName == "Month_07" || e.PropertyName == "Month_08" || e.PropertyName == "Month_09" || e.PropertyName == "Month_10" ||
                    e.PropertyName == "Month_11" || e.PropertyName == "Month_12")
                    this.OnPropertyChanged("Total");
            }
    
            /// <summary>
            /// Returns the total sales sum of all selected customers.
            /// </summary>
            public double? Total
            {
                get
                {
                    return this.PlanningTemplate.Sum(c=>c.Month_01 + c.Month_02 + c.Month_03+c.Month_04+c.Month_05+c.Month_06+c.Month_07+c.Month_08+c.Month_09+c.Month_10+c.Month_11+c.Month_12);
                   
                }
            }
    
            void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                //CalculateTotalAmount();
            }
    
            #endregion // Base Class Overrides
        }
    }
    
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using RBBH.Application;
    using System.ComponentModel;
    
    namespace RBBH.ViewModels.PlanningViewModel
    {
        public class PlanningTemplateViewModel : WorkspaceViewModel
        {
    
            readonly Model.PlanningTemplate _planningTemplate;
            public PlanningTemplateViewModel(Model.PlanningTemplate PlanningTemplateItems, IUIDataProvider userRepository, IToolManager toolManager = null)
                : base(toolManager)
            { 
                _planningTemplate = PlanningTemplateItems;
                _planningTemplate.PropertyChanged += PlanningTemplate_PropertyChanged;
            }
    
    
            #region Properties
    
           
            public string Gl_Account_Desc { get { return _planningTemplate.Gl_Account_Desc; }}
            public string Prod_Desc { get { return _planningTemplate.Prod_Desc; }}
            
            public const string Month01PropertyName = "Month_01";
            public Nullable<double> Month_01
            {
                get { return _planningTemplate.Month_01; }
                set
                {
                    if (value == _planningTemplate.Month_01)
                        return;
    
                    _planningTemplate.Month_01 = value;
                             State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_01");
                }
            }
    
            public const string Month02PropertyName = "Month_02";
            public Nullable<double> Month_02
            {
                get { return _planningTemplate.Month_02; }
                set
                {
                    if (value == _planningTemplate.Month_02)
                        return;
    
                    _planningTemplate.Month_02 = value;
                    State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_02");
           
                }
            }
    
            public const string Month03PropertyName = "Month_03";
            public Nullable<double> Month_03
            {
                get { return _planningTemplate.Month_03; }
                set
                {
                    if (value == _planningTemplate.Month_03)
                        return;
    
                    _planningTemplate.Month_03 = value;
                      State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_03");
                 }
            }
    
            public const string Month04PropertyName = "Month_04";
            public Nullable<double> Month_04
            {
                get { return _planningTemplate.Month_04; }
                set
                {
                    if (value == _planningTemplate.Month_04)
                        return;
    
                    _planningTemplate.Month_04 = value;
                        State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_04");
                }
            }
    
            public const string Month05PropertyName = "Month_05";
            public Nullable<double> Month_05
            {
                get { return _planningTemplate.Month_05; }
                set
                {
                    if (value == _planningTemplate.Month_05)
                        return;
    
                    _planningTemplate.Month_05 = value;
                    State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_05");
                }
            }
            public const string Month06PropertyName = "Month_06";
            public Nullable<double> Month_06
            {
                get { return _planningTemplate.Month_06; }
                set
                {
                    if (value == _planningTemplate.Month_06)
                        return;
    
                    _planningTemplate.Month_06 = value;
                        State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_06");
                }
            }
    
            public const string Month07PropertyName = "Month_07";
            public Nullable<double> Month_07
            {
                get { return _planningTemplate.Month_07; }
                set
                {
                    if (value == _planningTemplate.Month_07)
                        return;
    
                    _planningTemplate.Month_07 = value;
                     State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_07");
                }
            }
    
            public const string Month08PropertyName = "Month_08";
            public Nullable<double> Month_08
            {
                get { return _planningTemplate.Month_08; }
                set
                {
                    if (value == _planningTemplate.Month_08)
                        return;
    
                    _planningTemplate.Month_08 = value;
                    State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_08");
                }
            }
    
            public const string Month09PropertyName = "Month_09";
            public Nullable<double> Month_09
            {
                get { return _planningTemplate.Month_09; }
                set
                {
                    if (value == _planningTemplate.Month_09)
                        return;
    
                    _planningTemplate.Month_09 = value;
                          State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_09");
                  }
            }
    
            public const string Month10PropertyName = "Month_10";
            public Nullable<double> Month_10
            {
                get { return _planningTemplate.Month_10; }
                set
                {
                    if (value == _planningTemplate.Month_10)
                        return;
    
                    _planningTemplate.Month_10 = value;
                        State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_10");
                }
            }
            public const string Month11PropertyName = "Month_11";
            public Nullable<double> Month_11
            {
                get { return _planningTemplate.Month_11; }
                set
                {
                    if (value == _planningTemplate.Month_11)
                        return;
    
                    _planningTemplate.Month_11 = value;
                      State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_11");
                }
            }
    
            public const string Month12PropertyName = "Month_12";
            public Nullable<double> Month_12
            {
                get { return _planningTemplate.Month_12; }
                set
                {
                    if (value == _planningTemplate.Month_12)
                        return;
    
                    _planningTemplate.Month_12 = value;
    
                      State = ObjectState.Modified;
                    base.OnPropertyChanged("Month_12");
              
                }
            }
    
           
            public Nullable<double> Mid_01
            {
                get { return _planningTemplate.Mid_01; }
                set
                {
                    if (value == _planningTemplate.Mid_01)
                        return;
    
                    _planningTemplate.Mid_01 = value;
                    State = ObjectState.Modified;
                    base.OnPropertyChanged("Mid_01");
                }
            }
    
            public Nullable<double> Mid_02
            {
                get { return _planningTemplate.Mid_02; }
                set
                {
                    if (value == _planningTemplate.Mid_02)
                        return;
    
                    _planningTemplate.Mid_02 = value;
                     State = ObjectState.Modified;
                    base.OnPropertyChanged("Mid_02");
                }
            }
    
            private decimal _Total;
            public decimal Total
            {
                get { return _Total; }
                set
                {
                    if (value != _Total)
                    {
                        _Total = value;
                        base.OnPropertyChanged("Total");
                    }
                }
            }
    
    
    
    
    
    
            void PlanningTemplate_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                switch (e.PropertyName)
               {
                    case Month01PropertyName:
                    case Month02PropertyName:
                    case Month03PropertyName:
                    case Month04PropertyName:
                    case Month05PropertyName:
                    case Month06PropertyName:
                    case Month07PropertyName:
                    case Month08PropertyName:
                    case Month09PropertyName:
                    case Month10PropertyName:
                    case Month11PropertyName:
                    case Month12PropertyName:
                        CalculateTotal();
    
                        base.OnPropertyChanged(TotalPropertyName);
                        break;
                }
            }
    
            public ObjectState State { get; set; }
    
            #endregion Properties
    
    
            public enum ObjectState : int
            {
                Unchanged = 0,
                Added = 1,
                Modified = 2,
                Deleted = 3,
                Processed = 4
            }
    
        }
    
       
    }
        

    Wednesday, November 16, 2016 5:27 PM

All replies


  • Hi AlmirMulahasanovic,

    >>Where should I Subscribe to Calculate Total Property in my ViewModel

    From your description, you want the total property depends on the value of another, then needs to be raised for the total  property when the value of another property change.

    The below tutorial introduce about a calculated property called BoundStringProperty which provided a single Format method to return a formatted string. The formatted string value was re-evaluated whenever any of the dependencies changed. You can refer it.

    Rich property support in WPF viewModels (part 3):
    https://programmingwithpassion.wordpress.com/tag/inotifypropertychanged/

    The following post for your reference.

    Calculate result of two values on Property Changed MVVM:
    http://stackoverflow.com/questions/10808858/calculate-result-of-two-values-on-property-changed-mvvm


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Weiwei Cai Tuesday, November 22, 2016 9:43 AM
    Thursday, November 17, 2016 6:06 AM