Treeview MVVM ObservableCollection 更新
我有一个树视图,它绑定到许多嵌套的 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
和 中的
。Teams
使用 ObservableCollections 团队
中的个人
谢谢,
标记
I have a treeview which binds to lots of nested ObservableCollections. Each level of the treeview shows an aggregated sum of all the hours in child items. For example:
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"
When I modify my Individual.Hours
in my ViewModel class i want to update the hours
values in both my team and departments too.
I'm already using NotificationProperties
for all my Hours
properties, and ObservableCollections for Teams
in Departments
and Individuals
in Teams
.
Thanks,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个部门的工作时间取决于其团队的工作时间总和。每个团队的时间取决于其个人的总时间。因此,每个团队都应该侦听对其任何个人的
Hours
属性的更改。当检测到时,它应该为其自己的Hours
属性引发OnPropertyChanged
。同样,每个Department
都应该侦听其任何团队的Hours
属性的更改。当检测到时,它应该为其自己的Hours
属性引发OnPropertyChanged
。最终结果是任何个人(或团队)时间的更改都会反映在父级中。
伪代码可以通过重构得到极大的改进,但给出了答案的本质:
请注意,如果性能成为问题,您可以让集合本身维护小时总数。这样,每当孩子的
Hours
属性发生变化时,它就可以进行简单的加法,因为它被告知旧值和新值。因此,它知道应用于聚合的差异。Each department's hours depends on the aggregate of its team's hours. Each team's hours depends on the aggregate of its individual's hours. Thus, each Team should listen for changes to any of its individual's
Hours
property. When detected, it should raiseOnPropertyChanged
for its ownHours
property. Similarly, eachDepartment
should listen for changes to any of its team'sHours
property. When detected, it should raiseOnPropertyChanged
for its ownHours
property.The end result is that changing any individual's (or team's) hours is reflected in the parent.
Pseduo code that can be greatly improved with refactoring but gives the essence of the answer:
Note that if performance becomes an issue you can have the collection itself maintain the hour total. That way, it can do a simple addition whenever a child's
Hours
property changes, because it is told the old value and the new value. Thus, it knows the difference to apply to the aggregate.我担心您必须在每个个体的父级(“团队”)的父 ObservableCollection 容器上显式调用通知。
然后从单个父级中,为祖级父级(“部门”)设置通知事件。
I am affraid you will have to explicity call the notification on the parent ObservableCollection container for each individual's parent ('Team').
Then from the individual parent, set notification event for grand parent ('Department').