WPF - 良好的方式,采取列表,以树 [英] WPF - Good Way to take a list to a Tree

查看:113
本文介绍了WPF - 良好的方式,采取列表,以树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的列表:

I have a list that looks like this:


Base/Level1/Item1
Base/Level1/Item2
Base/Level1/Sub1/Item1
Base/Level2/Item1
Base/Level3/Sub1/Item1

我想一个简单的方法它放入一个ListView。 (即与此类似)

I would like an easy way put that into a ListView. (Ie similar to this)


Base
  |
  +->Level1
  |    |
  |    +=Item1
  |    +=Item2
  |    |
  |    +->Sub1
  |        |
  |        +=Item1
  |
  +->Level2
  |    |
  |    +=Item1

  |
  +->Level3
       |
       +->Sub1
           |
           +=Item1

有没有一个既定的方法,使这种转换或者我只是需要推出自己的解析器?

Is there an established way to make this kind of conversion or do I just need to roll my own parser?

(如果它可能是相关的在我的code真实的项目是TFS迭代路径。)

(In case it may be relevant the real items in my code are TFS Iteration Paths.)

推荐答案

这将您的字符串列表,并把它变成适合你描述的树视图查看树:

This will take your list of strings and turn it into a tree suitable for viewing with TreeView as you described:

public IList BuildTree(IEnumerable<string> strings)
{
  return
    from s in strings
    let split = s.Split("/")
    group s by s.Split("/")[0] into g  // Group by first component (before /)
    select new
    {
      Name = g.Key,
      Children = BuildTree(            // Recursively build children
        from s in grp
        where s.Length > g.Key.Length+1
        select s.Substring(g.Key.Length+1)) // Select remaining components
    };
}

这将返回匿名类型的树,每个都包含一个名称属性和一个儿童财产。这可以直接绑定到 TreeView控件通过指定 HierarchicalDataTemplate 的ItemsSource ={结合儿童}和内容由&LT的; TextBlock的文本={结合名}方式&gt; 或类似

This will return a tree of anonymous types, each containing a Name property and a Children property. This can be bound directly to the TreeView by specifying a HierarchicalDataTemplate with ItemsSource="{Binding Children}" and content consisting of a <TextBlock Text="{Binding Name}"> or similar.

另外,你可以,如果你想要更多的成员或语义定义code树节点类。例如,假定这个节点类:

Alternatively you could define a tree node class in code if you want additional members or semantics. For example, given this node class:

public class Node 
{
  public string Name { get; set; } 
  public List<Node> Children { get; set; } 
}

您BuildTree功能会稍有不同:

your BuildTree function would be slightly different:

public List<Node> BuildTree(IEnumerable<string> strings)
{
  return (
    from s in strings
    let split = s.Split("/")
    group s by s.Split("/")[0] into g  // Group by first component (before /)
    select new Node
    {
      Value = g.Key,
      Children = BuildTree(            // Recursively build children
        from s in grp
        where s.Length > g.Key.Length+1
        select s.Substring(g.Key.Length+1)) // Select remaining components
    }
    ).ToList();
}

这同样可以直接使用 HierarchicalDataTemplate 的约束。我一般用第一个解决方案(匿名类型),除非我想要做一些特别的树节点。

Again this can be bound directly using a HierarchicalDataTemplate. I generally use the first solution (anonymous types) unless I want to do something special with the tree nodes.

这篇关于WPF - 良好的方式,采取列表,以树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆