我应该在哪里订阅我的ViewModel中的计算总属性 [英] Where should I Subscribe to Calculate Total Property in my ViewModel

查看:51
本文介绍了我应该在哪里订阅我的ViewModel中的计算总属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有两个视图模型(PlanningAreaDimensionDetailsViewModel和PlanningTemplateViewModel)。请参阅下面的代码......

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

在PlanningTemplateViewModel中,我希望有一个总属性,它将反映属性month_01到month_12的变化。因此,在更改任何这些值之后,将重新计算总数。如果有人可以帮我并告诉我
(在下面调整我的代码)在哪里订阅以反映属性month_01到12的变化并重新计算总计,我将非常感激。

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.

目前我不知道如何使它有效...非常感谢Almir

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
        }

    }

   
}
    

推荐答案



您好AlmirMulahasanovic,


Hi AlmirMulahasanovic,

>>我应该在哪里订阅我的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.

下面的教程介绍了一个名为BoundStringProperty的计算属性,该属性提供了一个返回格式化字符串的Format方法。每当任何依赖项发生更改时,都会重新评估格式化的字符串值。您可以参考它。

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.

WPF viewModels中的丰富属性支持(第3部分):

https://programmingwithpassion.wordpress.com/tag/inotifypropertychanged/

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

以下帖子供您参考。

在Property Changed MVVM上计算两个值的结果:

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

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

Yohann Lu


这篇关于我应该在哪里订阅我的ViewModel中的计算总属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆