简体   繁体   中英

WPF treeview with heirarchical databinding using multiple data sources

I have this wpf structure:

 <TreeView x:Name="tree" ItemsSource="{Binding MainDirectory}"> 
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding _directory}">
                        <TreeViewItem Header="{Binding _name}" >
                        </TreeViewItem>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>

 </TreeView>

My directories display great but my _directory property also have a _file property that I want to display inside the treeview. Whenever I try to add another treeviewitem though WPF tells me that the visual tree is set more than once. Is there any way I can achieve this. Thank you

Here you go:

<TreeView x:Name="tree" ItemsSource="{Binding MainDirectory}"> 
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding _directory}">
                         <HierarchicalDataTemplate.ItemTemplate>
                              <HierarchicalDataTemplate ItemsSource="{Binding _file}">
                                   <TreeViewItem Header="{Binding _name}" />
                              </HierarchicalDataTemplate>
                         </HierarchicalDataTemplate.ItemTemplate>
                        <TreeViewItem Header="{Binding _name}" >
                        </TreeViewItem>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>

 </TreeView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM