2011-04-11 93 views
2

我在我的winforms程序中排序树视图的子节点时遇到问题。我的树视图由一些XML文件填充,它使用xml文件中的内部文本作为节点的Text属性(因此,我认为在将它们添加到树视图之前不能对它们排序,或者如果可能,因为xml文件是规模很大,我不想浪费这个过程)。在我的计划已填充的树视图看起来是这样的:在c#winforms中填充树视图后排序树视图的子节点

enter image description here

正如您可以猜到我想的子节点进行排序像(我不想HBM \ D10来HBM \ D1之后),而我想:

HBM\D1 
    HBM\D2 
    HBM\D3 
etc... 

我已经尝试treeView1.Sort(),并且还加入的BeginUpdate和endUpdate,但我没有suceess :(

我使用.NET 4,任何提示将appriciated

确定我sortet它用托马斯的建议是:

class NodeSorter : IComparer 
{ 
     public int Compare(object x, object y) 
     {   
      TreeNode tx = (TreeNode)x; 
      TreeNode ty = (TreeNode)y; 

      if (tx.Text.Length < ty.Text.Length) 
      { 
       return -1; 
      } 

      if (tx.Text.Length > ty.Text.Length) 
      { 
       return 1; 
      } 

      return 0; 
     } 
} 

回答

8

您需要创建一个自定义比较,并将其分配给TreeViewNodeSorter属性:

public class NodeSorter : System.Collections.IComparer 
{ 
    public int Compare(object x, object y) 
    { 
     TreeNode tx = (TreeNode)x; 
     TreeNode ty = (TreeNode)y; 

     // Your sorting logic here... return -1 if tx < ty, 1 if tx > ty, 0 otherwise 
     ... 
    } 
} 


... 

treeView.TreeViewNodeSorter = new NodeSorter(); 
treeView.Sort(); 
+0

谢谢,你能不能给多一点关于逻辑的一部分?是关于将节点转换为二进制还是其他? – 2011-04-11 08:27:49

+0

@ Sean87,您需要按照您自己的逻辑对TreeNode的文本进行排序,即提取文本的数字部分,然后通过数字进行比较,如果名称的其余部分相同 – 2011-04-11 09:24:23

+0

为了使其工作。净4(Windows 8)我不得不使它从比较器继承并实现比较作为公共覆盖int比较(TreeNode Node1,TreeNode Node2)' – ThunderGr 2013-10-17 12:30:08

2

您使用的字母排序,所以D10自带D1之后。
您应该尝试对丢弃“D”字符进行排序,并将其余字符串转换为数字。

2

我已经写了一些自定义的comparers使创建你需要在这里比较容易做的比较器:MultiComparerProjectionComparer。在一起,你可以创建一个比较器来排序你需要的动态,而无需手动创建一个类。我在这里提供的实际上并不是我写这些类的方式,为了简洁起见我删掉了很多代码(尽管留下了一些助手更容易使用)。

要创建比较器:

var comparer = OrderedComparer.Create(
    ProjectionComparer.Create((TreeNode tn) => tn.Text.Substring(0, 1)), 
    ProjectionComparer.Create((TreeNode tn) => Convert.ToInt32(tn.Text.Substring(1))) 
); 
treeView.TreeViewNodeSorter = comparer; 

及其类别:

public static class OrderedComparer 
{ 
    public static OrderedComparer<TSource> Create<TSource>(params IComparer<TSource>[] comparers) 
    { return new OrderedComparer<TSource>(comparers); } 
} 
public static class ProjectionComparer 
{ 
    public static ProjectionComparer<TSource, TKey> Create<TSource, TKey>(Func<TSource, TKey> keySelector) 
    { return new ProjectionComparer<TSource, TKey>(keySelector); } 
} 
public sealed class OrderedComparer<TSource> : Comparer<TSource> 
{ 
    public OrderedComparer(params IComparer<TSource>[] comparers) 
    { 
     this.comparers = comparers.ToArray(); 
    } 
    private IComparer<TSource>[] comparers; 

    public override int Compare(TSource x, TSource y) 
    { 
     var cmp = 0; 
     foreach (var comparer in comparers) 
      if ((cmp = comparer.Compare(x, y)) != 0) 
       break; 
     return cmp; 
    } 
} 
public sealed class ProjectionComparer<TSource, TKey> : Comparer<TSource> 
{ 
    public ProjectionComparer(Func<TSource, TKey> keySelector) 
    { 
     this.keySelector = keySelector; 
     this.keyComparer = Comparer<TKey>.Default; 
    } 
    private Func<TSource, TKey> keySelector; 
    private IComparer<TKey> keyComparer; 

    public override int Compare(TSource x, TSource y) 
    { 
     var xKey = keySelector(x); 
     var yKey = keySelector(y); 
     return keyComparer.Compare(xKey, yKey); 
    } 
} 
+0

已更新至更新版本的代码(仍有所修剪)。 – 2011-04-18 21:45:18