【问题标题】:Treeview MVVM ObservableCollection UpdatesTreeview MVVM ObservableCollection 更新
【发布时间】:2010-01-16 20:23:02
【问题描述】:

我有一个绑定到许多嵌套 ObservableCollections 的树视图。树视图的每个级别都显示子项目中所有小时的汇总总和。例如:

Department 1, 10hrs
  ├ Team 10, 5hrs  
  │  ├ Mark, 3hrs
  │  └ Anthony, 2hrs   
  └ Team 11, 5hrs
     ├ Jason, 2hrs
     ├ Gary, 2hrs
     └ Hadley, 1hrs  
Department 2, 4hrs   
  ├ Team 20, 3hrs  
  │  ├ Tabitha, 0.5hrs
  │  ├ Linsey, 0.5hrs
  │  └ Guy, 2hrs
  └ Team 11, 1hr
     └ "Hadley, 1hr"  

当我在 ViewModel 类中修改 Individual.Hours 时,我想更新 hours 我的团队和部门的价值观也是如此。

我已经将NotificationProperties 用于我所有的Hours 属性,并为Departments 中的TeamsIndividuals 中的Teams 使用ObservableCollections。

谢谢,
标记

【问题讨论】:

    标签: wpf silverlight-3.0 mvvm observablecollection


    【解决方案1】:

    每个部门的工作时间取决于其团队工作时间的总和。每个团队的小时数取决于其个人小时数的总和。因此,每个团队都应该监听其任何个人的Hours 属性的更改。检测到时,它应该为自己的Hours 属性提高OnPropertyChanged。同样,每个Department 都应侦听对其团队的任何Hours 属性的更改。检测到时,它应该为自己的Hours 属性提升OnPropertyChanged

    最终结果是更改任何个人(或团队)的时间都会反映在父级中。

    可以通过重构大大改进但给出答案的本质的伪代码:

    public class Individual : ViewModel
    {
        public int Hours
        {
            // standard get / set with property change notification
        }
    
    }
    
    public class Team : ViewModel
    {
        public Team()
        {
            this.individuals = new IndividualCollection(this);
        }
    
        public ICollection<Individual> Individuals
        {
            get { return this.individuals; }
        }
    
        public int Hours
        {
            get
            {
                // return sum of individual's hours (can cache for perf reasons)
            }
        }
    
        // custom collection isn't strictly required, but makes the code more readable
        private sealed class IndividualCollection : ObservableCollection<Individual>
        {
            private readonly Team team;
    
            public IndividualCollection(Team team)
            {
                this.team = team;
            }
    
            public override Add(Individual individual)
            {
                individual.PropertyChanged += IndividualPropertyChanged;
            }
    
            public override Remove(...)
            {
                individual.PropertyChanged -= IndividualPropertyChanged;
            }
    
            private void IndividualPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "Hours")
                {
                    team.OnPropertyChanged("Hours");
                }
            }
        }
    }
    
    public class Department : ViewModel
    {
        public Department()
        {
            this.teams = new TeamCollection();
        }
    
        public ICollection<Team> Teams
        {
            get { return this.teams; }
        }
    
        public int Hours
        {
            get
            {
                // return sum of team's hours (can cache for perf reasons)
            }
        }
    
        // TeamCollection very similar to IndividualCollection (think generics!)
    }
    

    请注意,如果性能成为问题,您可以让集合本身保持小时总数。这样,只要孩子的Hours 属性发生变化,它就可以进行简单的加法,因为它被告知旧值和新值。因此,它知道应用于聚合的差异。

    【讨论】:

      【解决方案2】:

      我担心您必须为每个人的父级(“团队”)显式调用父级 ObservableCollection 容器上的通知。

      然后从单个父母,为祖父母('部门')设置通知事件。

      Team.OnPropertyChanged("Individuals")
      

      【讨论】:

      • 谢谢,我以为是这样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2023-04-06
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多