我如何在一个树视图中获得节点级别
本文关键字:视图 节点 一个 | 更新日期: 2023-09-27 18:05:50
我正在使用来自另一个web的代码:
如何在数据库中对该类进行建模?
我在每个目标记录中都有一个名为"Rank"的字段。它告诉我位置是什么。例如:
Objective "Geometry": Rank1
|_Objective "Squares": Rank1
|_Objective "Circles": Rank2
|_Objective "Triangle": Rank3
|_Objective "Types": Rank1
Objectve "Algebra": Rank2
Objective "Trigonometry": Rank3
这个秩告诉我节点的顺序。但是我想要得到所有的排名:对于第三个位置将是:
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。我该怎么做呢?
<TreeView Name="treeView1">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:Objective}" ItemsSource="{Binding Path=Objectives}" >
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我不确定我是否理解你想要的,但是递归地遍历树并为对象分配秩是非常简单的。下面是我快速编写的一些代码:
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
开头:
void main()
{
foreach(Node root in nodeCollection)
{
this.Recursion(root, 1);
}
}
void Recursion(Node n, int depth)
{
node.Depth = depth;
foreach(Node child in n.Children)
{
this.Recursion(child, depth++);
}
}
这将给你一个深度为每个节点(当然你必须添加字段节点到你的类)。
到目前为止,实现这一点的最简单方法是在模型或视图模型中。例如,在Node
类中,您可以实现以下属性:
public Collection<Node> Siblings { /* see below */ }
public Collection<Node> Children { get; set; }
public Node Parent { get; set; }
public int Position
{
get
{
return (Parent == null)
? 0 // I don't like magic numbers, but I don't want to make this an int? either
: Siblings.IndexOf(this) + 1;
}
}
public string Rank
{
get
{
return (Parent == null)
? Position.ToString()
: Parent.Rank + "." + Position.ToString();
}
}
通常,实现Siblings
属性最简单的方法是
public Collection<Node> Siblings
{
get
{
return (Parent == null)
? null
: Parent.Children;
}
}
在存在不属于节点层次结构的顶级节点集合的情况下不起作用,就像这样。您可以通过创建一个不出现在您的UI中的根Node
对象来模拟它——在这种情况下,您将把TreeView
绑定到根节点的Children
属性,并像这样实现Rank
:
public string Rank
{
get
{
if (Parent == null)
{
return null;
}
if (Parent.Parent == null)
{
return Position.ToString();
}
return Parent.Rank + "." + Position.ToString();
}
}