0

I need some help with HierarchicalDataTemplate... I am trying to build a TreeView to display some hierarchical data like this:

  • RuleSet <- (root) -RuleA
    • RuleB
      • RuleC
      • RuleA .....
    • RuleD

RuleA, ... are derived from the same RuleBase that has a

  • Type
  • RuleBase[] Rules

RuleSet has

  • Name
  • List

my code as far as I get:

<TreeView x:Name="ruleSetTree" Margin="0,10,0,0" ItemsSource="{Binding Path=SelectedTypeRuleSet>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate  DataType="{x:Type engine:RuleSet}">
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate x:Name="leafTemplate"
                              ItemsSource="{Binding Path=Rules}" 
                              DataType="{x:Type engine:RuleBase}">
                            <TextBlock Text="{Binding Path=Name}"/>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                    <TextBlock x:Name="hierarchyItem" Text="{Binding Path=TargetType}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
 </TreeView>

What I see now is the Name of RuleSet. The toggle button of TreeView is hidden. I deserialize an xml into my RuleSet model to get the data for this TreeView. The RuleSet is filled in correctly...can anyone give a hand please?

Thank you!

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
Victor
  • 260
  • 1
  • 3
  • 13

2 Answers2

0

There is no ItemsSource specified in the first HierarchicalDataTemplate. Shouldn't you bind it to the List property of your RuleSet ?

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
0

Why do you nest another hierarchical data template into the existing one? That may be the mistake. Especially because you didn't specify an ItemSource in your first data template. If all the nodes are of type RuleSet you can do it like this:

<TreeView x:Name="ruleSetTree" Margin="0,10,0,0" ItemsSource="{Binding Path=SelectedTypeRuleSet>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type engine:RuleSet}"
                                      ItemsSource="{Binding Path=Rules}">
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}"/>
                    <TextBlock Text="{Binding Path=TargetType}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

UPDATE: Here's an updated version, which should match your requirements. This just works, though, if all the child nodes are of type RuleBase

<UserControl.Resources>
        <HierarchicalDataTemplate x:Key="RuleBaseTemplate"
                                  ItemsSource="{Binding Rules}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="RuleSetTemplate" 
                                  ItemsSource="{Binding Rules}" 
                                  ItemTemplate="{StaticResource RuleBaseTemplate}">
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding TargetType}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
</UserControl.Resources>
<!-- rest of the code -->
<TreeView x:Name="ruleSetTree" Margin="0,10,0,0" 
          ItemsSource="{Binding SelectedTypeRuleSet}"
          ItemTemplate="{StaticResource RuleSetTemplate}"/>
Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
  • The first node is a `RuleSet`, child nodes are `RuleBase`. – Nicolas Repiquet May 10 '12 at 07:31
  • So, the first related items are: RuleSet with Rule (one to many relationship) then we have: Rule - Rule(one to many relation ship). I can see in TreeView the RuleSet. The hierarchy is the template for TreeView item, and tree view allready has the ItemsSource assigned...The Htemplate seems to share it... – Victor May 10 '12 at 10:00
  • One question: is the root element `Rule A` the only item of type `RuleSet`? – Michael Schnerring May 10 '12 at 10:23
  • No (My mistake - "bad drawing" -> it got messed out while posting).The hierarchy is: 1 RuleSet(But I put as a collection with ONE item to be able to put it as data source to first hierarchy). So 1 RuleSet that has(1..n)Rules and each Rule can have (1..n) other Rules and so on – Victor May 10 '12 at 16:26
  • Could you draw a bigger example tree with the type of each node behind it, please? – Michael Schnerring May 11 '12 at 06:13
  • The bigger draw is in first post. All this mess is a tree that starts from a RuleSet and have rules, the rules can have other rules and other rules ... – Victor May 31 '12 at 07:46