当前位置: 动力学知识库 > 问答 > 编程问答 >

xaml - Show multiple types in tree view with labels wpf

问题描述:

I am trying to show multiple types in the tree view

I have Projects, which contain workspaces

When Projects are expanded can see list of workspaces.

I have tried using <TreeViewItem Header="My Projects" ItemSource="{Binding Projects}"> but "My Projects" is becoming a root node and Project1, Project2... becomes its child nodes.

Also I have tried a Label with "My Projects" inside the treeview but could get to show as needed.

So Finally I ended up with a tree view for Projects and Followed by another tree view for Workspaces.

Is it possible to implement this in a single tree view?

网友答案:

Try adding different HierarchicalDataTemplates for Project type and Workspace type separately instead of directly creating TreeViewItems inside:

<TreeView ItemsSource="{Binding Projects}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="model:Project (Whatever type you are using for Project)" ItemsSource="{Binding Workspaces}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="model:Workspace (Whatever type you are using for Workspace)">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
分享给朋友:
您可能感兴趣的文章:
随机阅读: