WPF Treeview (with Children) move item up and down -


i have problem movement of treeitem in wpf treeview. here item of problem

public class workflow  {      public string name { get; set; }     public int rank { get; set; }      private observablecollection<workflow> childworkflowsvalue = new observablecollection<workflow>();     public observablecollection<workflow> childworkflows {       {         return childworkflowsvalue;       }       set {         childworkflowsvalue = value;       }     }      public workflow() { }      public workflow(string name, int rank) {       name = name;       rank = rank;     } } 

and logic of movement

private void fillwithdata() {    myworkflow.add(new workflow("airbag ausgelöst", 0));   myworkflow.add(new workflow("abstand halten", 1));   workflow subworkflow = new workflow("vorgehen", 2);   subworkflow.childworkflows.add(       new workflow("innenraum kontrollieren", 0));   subworkflow.childworkflows.add(       new workflow("spuren des airbags suchen", 1));   subworkflow.childworkflows.add(       new workflow("airbag zur seite drücken", 2));   myworkflow.add(subworkflow);   mytreeview.datacontext = myworkflow; }  private void btnmoveup_click(object sender, routedeventargs e) {    var currentwf = this.mytreeview.selecteditem workflow;    if (currentwf != null) {      findrecursive(myworkflow, currentwf.name);   }  }  private void findrecursive(observablecollection<workflow> wf, string searchtext) {    foreach (var item in wf) {     if (item.name == searchtext) {         wf.move(item.rank, item.rank-1);       //problem in here     }      findrecursive(item.childworkflows, searchtext);   } } 

when try move items compiler says: not allowed modify collection. searched web solution no chance. may there better way this.

you cannot modify collection while iterating through using foreach.

i avoid kind of problem using 2 different ways:

var itemtomove; foreach (var item in wf) {     if (item.name == searchtext) {        itemtomove = item     }      findrecursive(item.childworkflows, searchtext);   } wf.move(itemtomove.rank, itemtomove.rank-1); 

or

observablecollection<workflow> wfclone = new observablecollection<workflow>(wf); foreach (var item in wfclone) {     if (item.name == searchtext) {         wf.move(item.rank, item.rank-1);     }      findrecursive(item.childworkflows, searchtext);   } 

using kind of "trick" you'll able modify list :)


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -