简体   繁体   English

WPF TreeView 具有多个项目来源

[英]WPF TreeView with multiple item sources

I am stuck on a problem with the wpf treeview control in my current application.我在当前应用程序中遇到了 wpf treeview 控件的问题。 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.现在我的问题是我无法在我的 treeview 中显示这个结构。

I'd like to have the following structure:我想有以下结构:

+ Project
  - Folder
    -- Folder
    -- 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?你试过 HierarchicalDataTemplate 吗? You can find solution here: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx您可以在这里找到解决方案: 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.由于您总是提到您需要多个 ItemsSource,我认为您的问题是您的项目 object 有两个 collections:文件和文件夹,您不知道如何将它们都放入 HierarchicalDataTemplate 的 ItemsSource。

The solution to that is the CompositeCollection .解决方案是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.请注意,如果您在 XAML 中构造它,则CollectionContainers内将没有 DataContext,因此您无法轻松绑定Collection属性。

You'll have to use a DataTemplateSelector , see the example here:您必须使用DataTemplateSelector ,请参见此处的示例:

http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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