HierarchicalDataTemplate它应该做什么?

时间:2014-09-10 23:48:31

标签: c# wpf treeview hierarchicaldatatemplate

基本上我有一个eeview,其中有一堆对象,treeview需要绑定相同的属性。

这里是使用的代码,它似乎无法正常工作

    <TreeView ItemsSource="{Binding Drives}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate>
                <TreeViewItem Header="{Binding Name}" ItemsSource="{Binding Folders}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}">
                    <TreeViewItem.ItemTemplate>
                        <HierarchicalDataTemplate>
                            <StackPanel>
                                <TreeViewItem Header="{Binding Name}" ItemsSource="{Binding Folders}" IsExpanded="{Binding IsExpanded,Mode=TwoWay}">
                                    <TreeViewItem.ItemTemplate>
                                        <HierarchicalDataTemplate>
                                            <TreeViewItem Header="{Binding Name}" ItemsSource="{Binding Folders}" IsExpanded="{Binding IsExpanded,Mode=TwoWay}"/>
                                        </HierarchicalDataTemplate>
                                    </TreeViewItem.ItemTemplate>
                                </TreeViewItem>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeViewItem.ItemTemplate>
                </TreeViewItem>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

基本上当我到达第三级的treeview项时,我只得到Namespace + ObjectName。 DataTemplate不应该继续流动吗?

1 个答案:

答案 0 :(得分:1)

更像这样

    <TreeView ItemsSource="{Binding Drives}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Folders}">
                <TextBlock Text="{Binding Name}"></TextBlock>
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}" />
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>