views:

20

answers:

1

I am trying to map a TreeView to a collection using the HierarchicalDataTemplate. The collection contains an Object that contains child entities of itself(Many To Many Relation), and then goes down to another object using regular one to many relation. I use the followings:

<HierarchicalDataTemplate DataType="{x:Type src:Organization}" ItemsSource="{Binding Path=ChildOrgs}" >
            <StackPanel>
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type src:Organization}" ItemsSource="{Binding Path=Units}" >
            <StackPanel>
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
</HierarchicalDataTemplate>

I am using the 'ChildOrgs' collection to retrieve 'Organzation' childs from an Organization. This works perfectly. My problem is that i want the treeview to keep drilling in to the Organizations 'Unit' collection. But i get the following error: "Item has already been added..." on the 'Organization' entity...

Will appriciate any ideas, Many Thanks,

+1  A: 

Switch the first definition to:

<HierarchicalDataTemplate DataType="{x:Type src:Organization}"
    ItemsSource="{Binding Path=ChildOrgs}"
    ItemTemplate="{DynamicResource ChildOrgTemplate}">
    <StackPanel>
        <TextBlock Text="{Binding Path=Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ChildOrgTemplate"
    ItemsSource="{Binding Path=Units}">
    <StackPanel>
        <TextBlock Text="{Binding Path=Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>
sixlettervariables
Thank you! Your solution is in the right way! I just needed to change the DynamicResource to a staticResource as i got an exception. And ofcourse i had to locate the ChildOrgTemplate first. Now it works perfectly! Thank you.
OrPaz