开发者

WPF Treeview HierarchicalDataTemplate MTM Relation

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 relat开发者_运维技巧ion. 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,


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜