2011-08-16 98 views
0

我使用這個代碼從另一個網站:如何獲取treeView中的節點級別?

How can I model this class in a database?

我在每個目標記錄的字段命名爲「排名」。它告訴我什麼位置。例如:

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> 
+0

我建議你通過某種遞歸方法在模型中(而不是在視圖/ GUI中)解決這個問題。 –

+0

有關設置級別的遞歸函數的示例,請參閱:http://stackoverflow.com/questions/6225123/simulating-cte-recursion-in-c – woggles

回答

1

我不知道如果我理解你想要什麼,但它是非常簡單的遞歸遍歷樹並指定行列的對象。下面是一些簡單的代碼我颳起了:使用這個類

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 
0

開始的東西,如:

這會給你是每個節點的深度(當然你有e將字段Node添加到您的班級)。

0

到目前爲止,最簡單的實現方法是在模型或視圖模型中。例如,在你的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(); 
    } 
} 
相關問題