2011-08-16 17 views
0

私は別のWebからこのコードを使用しています:私は、各客観レコードに「ランク」という名前のフィールドを持っているツリービューでどのようにノードレベルを取得できますか?

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 

このランクはノードの順序を示します。しかし、私はすべてのランクを取得したい:3番目のポジションのために:

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

スタート:

これは与えますあなたは各ノードのための深さ(もちろんあなたはhav eノードをクラスに追加します)。

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オブジェクトを作成することによって、これをダミーすることができます - このケースでは、ルートノードのChildrenプロパティにTreeViewを結合し、このようなRankを実装したい:

public string Rank 
{ 
    get 
    { 
     if (Parent == null) 
     { 
      return null; 
     } 
     if (Parent.Parent == null) 
     { 
      return Position.ToString(); 
     } 
     return Parent.Rank + "." + Position.ToString(); 
    } 
} 
関連する問題