简体   繁体   English

在TreeView中关闭对分支的排序

[英]Turn off sorting for branch in a TreeView

I have a Winforms Treeview with nodes in several levels and branches. 我有一个Winforms Treeview,其中的节点位于多个级别和分支中。 The position of the nodes in the tree are important. 树中节点的位置很重要。 I need to have some branches unsorted, while the rest of the nodes in the treeview sorted. 我需要一些分支未排序,而树视图中的其余节点已排序。 I also need to be able to turn the sorting on/off by code according to user input. 我还需要能够根据用户输入通过代码打开/关闭排序。

From what I can read and google the winform treeview component can only be sorted or unsorted as a whole. 根据我的阅读和搜索,winform treeview组件只能整体排序或不排序。 All or nothing. 全部或全无。 Is that correct, so I have to write the sorting mecanism myself, or did I miss something? 是正确的,所以我必须自己编写排序机制,还是错过了什么?

I don't know about ignoring branches when it comes to sorting, but if you just want to sort a single branch, you can try this old school method: 我不知道在排序时会忽略分支,但是如果您只想对单个分支进行排序,则可以尝试以下旧方法:

private void SortBranch(TreeNode parentNode) {
  TreeNode[] nodes;
  if (parentNode == null) {
    nodes = new TreeNode[treeView1.Nodes.Count];
    treeView1.Nodes.CopyTo(nodes, 0);
  } else {
    nodes = new TreeNode[parentNode.Nodes.Count];
    parentNode.Nodes.CopyTo(nodes, 0);
  }
  Array.Sort(nodes, new TreeSorter());
  treeView1.BeginUpdate();
  if (parentNode == null) {
    treeView1.Nodes.Clear();
    treeView1.Nodes.AddRange(nodes);
  } else {
    parentNode.Nodes.Clear();
    parentNode.Nodes.AddRange(nodes);
  }
  treeView1.EndUpdate();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM