I'm using this code from the another web:
How can I model this class in a database?
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
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>
By far the easiest way to implement this is in the model or view model. For instance, in your
Node
class, you could implement the following properties:Usually, the simplest way to implement a
Siblings
property isThat doesn't work in the case where there is a collection of top-level nodes that's not part of the node hierarchy, like this. You can dummy this up by creating a root
Node
object that doesn't appear in your UI - in this case, you'd bind theTreeView
to the root node'sChildren
property, and implementRank
like this: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:
using this class:
output:
Start with something like:
This will give you a Depth for each node (of course you have to add the field Node to your class).