WPF TreeView with multiple item sources
I am stuck on a problem with the wpf treeview control in my current application. I'm building a visual studio like application (editor with project tree) for my school.
Now i have the problem that I can't display this structure in my treeview.
I'd like to have the following structure:
+ Project
- Folder
-- Folder
开发者_JS百科 -- File
- Folder
- File
+ Project
...
I've tried a lot of things I found around the internet, but not one of them really did work..
Thanks for your help.
Have you tried HierarchicalDataTemplate ? You can find solution here : http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
Since you always mention that you need multiple ItemsSources i think your problem is that your project object has two collections: Files & Folders and you don't know how to put both of them into the HierarchicalDataTemplate's ItemsSource.
The solution to that is the CompositeCollection
. Note that if you construct it in XAML you will not have a DataContext inside the CollectionContainers
, so you cannot bind the Collection
property that easily.
You'll have to use a DataTemplateSelector
, see the example here:
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector
精彩评论