2011-08-16 2 views
0

는 내가 다른 웹에서이 코드를 사용하고 있습니다 : I는 각 목적 레코드의 필드가 "순위"라는 한트리 뷰에서 노드 레벨을 얻으려면 어떻게해야합니까?

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

I을 일부 재귀 적 방법으로 모델 (보기/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(); 
    } 
} 
관련 문제