izzufarg
izzufarg

Reputation: 3

WPF Treeview (with Children) move item up and down

i have a problem with the movement of a TreeItem in a WPF TreeView. Here is the Item of my 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 {
      get {
        return childWorkflowsValue;
      }
      set {
        childWorkflowsValue = value;
      }
    }

    public Workflow() { }

    public Workflow(string name, int rank) {
      Name = name;
      Rank = rank;
    }
}

and the logic of the 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 as 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 i try to move the items the compiler says: it is not allowed to modify the collection. I searched also the web for a solution but no chance. May be there is a better way to do this.

Upvotes: 0

Views: 2520

Answers (1)

Damascus
Damascus

Reputation: 6651

You cannot modify a collection while you are iterating through it using a foreach.

I usually avoid this kind of problem using two 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 this kind of "trick" you'll be able to modify the list :)

Upvotes: 1

Related Questions