wpf - Treeview MVVM ObservableCollection Updates -


i have treeview binds lots of nested observablecollections. each level of treeview shows aggregated sum of hours in child items. 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 modify individual.hours in viewmodel class want update hours values in both team , departments too.

i'm using notificationproperties hours properties, , observablecollections teams in departments , individuals in teams.

thanks,
mark

each department's hours depends on aggregate of team's hours. each team's hours depends on aggregate of individual's hours. thus, each team should listen changes of individual's hours property. when detected, should raise onpropertychanged own hours property. similarly, each department should listen changes of team's hours property. when detected, should raise onpropertychanged own hours property.

the end result changing individual's (or team's) hours reflected in parent.

pseduo code can improved refactoring gives essence of answer:

public class individual : viewmodel {     public int hours     {         // standard / set property change notification     }  }  public class team : viewmodel {     public team()     {         this.individuals = new individualcollection(this);     }      public icollection<individual> individuals     {         { return this.individuals; }     }      public int hours     {                 {             // return sum of individual's hours (can cache perf reasons)         }     }      // custom collection isn't strictly required, makes 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     {         { return this.teams; }     }      public int hours     {                 {             // return sum of team's hours (can cache perf reasons)         }     }      // teamcollection similar individualcollection (think generics!) } 

note if performance becomes issue can have collection maintain hour total. way, can simple addition whenever child's hours property changes, because told old value , new value. thus, knows difference apply aggregate.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -