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
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
Hoursproperty. When detected, it should raiseOnPropertyChangedfor its ownHoursproperty. Similarly, eachDepartmentshould listen for changes to any of its team’sHoursproperty. When detected, it should raiseOnPropertyChangedfor its ownHoursproperty.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
Hoursproperty changes, because it is told the old value and the new value. Thus, it knows the difference to apply to the aggregate.