WPF Treeview(带子项)上下移动项目

发布于 2024-10-31 13:48:31 字数 1722 浏览 4 评论 0原文

我在 WPF TreeView 中移动 TreeItem 时遇到问题。 这是我的问题的项目

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;
    }
}

和移动的逻辑

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);
  }
}

当我尝试移动项目时,编译器说:不允许修改集合。 我也在网上搜索了解决方案,但没有机会。 可能有更好的方法来做到这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-11-07 13:48:31

当您使用 foreach 迭代集合时,您无法修改集合。

我通常使用两种不同的方法来避免此类问题:

var itemToMove;
foreach (var item in wf) {
    if (item.Name == searchText) {
       itemToMove = item
    }

    FindRecursive(item.ChildWorkflows, searchText);
  }
wf.Move(itemToMove.Rank, itemToMove.Rank-1);

或者

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);
  }

使用这种“技巧”,您将能够修改列表:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文