如何在treeView中获取节点级别? [英] How can I get the node level in a treeView?

查看:292
本文介绍了如何在treeView中获取节点级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从另一个网站使用此代码:

I'm using this code from the another web:

如何在数据库中对该类进行建模?

我有在每个目标中,记录一个名为 Rank的字段。它告诉我什么位置。例如:

I have in each objective record a field named "Rank". It tells me what position is. For instance:

Objective "Geometry": Rank1
|_Objective "Squares": Rank1
|_Objective "Circles": Rank2
|_Objective "Triangle": Rank3
  |_Objective "Types": Rank1
Objectve "Algebra": Rank2
Objective "Trigonometry": Rank3

这个等级告诉我节点的顺序。但我想获得所有排名:第三名将是:

That rank tells me the order of the nodes. But I want to get all the rank: For the third position will be:

Objective "Geometry": Rank1
|_Objective "Squares": Rank1   -> 1.1
|_Objective "Circles": Rank2
|_Objective "Triangle": Rank3
  |_Objective "Types": Rank1   -> 1.3.1
Objectve "Algebra": Rank2
Objective "Trigonometry": Rank3    -> 3

我正在使用LINQ to SQL。我该怎么做?

I'm using LINQ to SQL. How can I do that?

    <TreeView Name="treeView1">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type data:Objective}" ItemsSource="{Binding Path=Objectives}" >
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>


推荐答案

我不确定我是否明白你想要什么,但是以递归方式遍历树并为对象分配等级非常简单。这是我快速编写的一些快速代码:

I'm not sure if I understand what you want, but it's pretty straightforward to walk the tree recursively and assign ranks to the objects. Here's some quick code I whipped up:

    public void Test()
    {
        Objective math = Init();
        RankObjective("", math);
        System.Console.ReadLine();
    }

    private void RankObjective(string rank, Objective objective)
    {
        int count = 1;
        if (!String.IsNullOrEmpty(rank))
            Console.WriteLine(objective.Name + ": " + rank);
        foreach (Objective child in objective.Objectives)
        {
            string newRank = String.IsNullOrEmpty(rank) ? count.ToString() : rank + "." + count.ToString();
            RankObjective(newRank, child);
            count++;
        }
    }

    private Objective Init()
    {
        Objective math = new Objective("Math");
        Objective geometry = new Objective("Geometry");
        geometry.Objectives.Add(new Objective("Squares"));
        geometry.Objectives.Add(new Objective("Circles"));
        Objective triangle = new Objective("Triangle");
        triangle.Objectives.Add(new Objective("Types"));
        geometry.Objectives.Add(triangle);
        math.Objectives.Add(geometry);
        math.Objectives.Add(new Objective("Algebra"));
        math.Objectives.Add(new Objective("Trigonometry"));
        return math;
    }

使用此类:

public class Objective
{
    public Objective(string name)
    {
        Name = name;
        Objectives = new List<Objective>();
    }

    public string Name { get; set; }
    public List<Objective> Objectives { get; set; }
}

输出:

Geometry: 1
Squares: 1.1
Circles: 1.2
Triangle: 1.3
Types: 1.3.1
Algebra: 2
Trigonometry: 3

这篇关于如何在treeView中获取节点级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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