简体   繁体   English

treeview在wpf中的多重绑定

[英]treeview Multibinding in wpf

I want to bind a treeview to a class like this one: 我想将树视图绑定到类似这样的类:

public class Folder : Base_FileFolder
{
    public Folder()
    {
        Folders = new ObservableCollection<Folder>();
        Files = new ObservableCollection<File>();
    }
    public ObservableCollection<Folder> Folders { get; set; }
    public ObservableCollection<File> Files { get; set; }
}

the other classes ares: 其他类是:

public class File : Base_FileFolder
{
}

public class Base_FileFolder : DependencyObject
{
    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
    public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Base_FileFolder), new UIPropertyMetadata(""));
}

How can I create a treeview that shows Files and Folders collection 如何创建显示“文件和文件夹”集合的树视图

I want to use something like this: 我想用这样的东西:

 <HierarchicalDataTemplate
 DataType="{x:Type model:Folder}"
 ItemsSource="{Binding Childs}">   
 <DockPanel>
       <Label Content="{Binding Name}"/>    </DockPanel>
 </HierarchicalDataTemplate>

so I get Somethign like this: 所以我得到像这样的Somethign:

rootFolder 根文件夹

|
|-File
|-File
|-Folder
  |-File
  |-File
  |-Folder
    |-File

What exactly is your question? 你究竟是什么问题? How to combine them? 如何结合它们? CompositeCollection . CompositeCollection

EDIT: as mentioned in the comments, my Intuipic application does something very similar to what you're requesting. 编辑:正如评论中所提到的,我的Intuipic应用程序执行的操作非常类似于您的请求。 Here's a screenshot: 这是一个截图:

替代文字

This is quite easy, considering your constellation. 考虑到你的星座,这很容易。

First: Adjust your classes. 第一:调整你的课程。 You do not need two separate Lists for files and folders in the folders class. 文件夹类中的文件和文件夹不需要两个单独的列表。 Just use one IList<Base_FileFolder> inside the Base_FileFolder class (good OOP) and call it Children! 只需在Base_FileFolder类(好的OOP)中使用一个IList<Base_FileFolder>并将其命名为Children!

Then you'll need only two more steps: 然后你只需要两个步骤:

  1. Two HierarchicalDataTemplates 两个HierarchicalDataTemplates

     <HierarchicalDataTemplate DataType="{x:Type FolderNode}" ItemsSource="{Binding Path=Children}"> <Grid> <TextBlock Text="{Binding FolderName}" /> </Grid> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type FileNode}" ItemsSource="{Binding Path=Children}"> <Grid> <TextBlock Text="{Binding FileName}" /> </Grid> </HierarchicalDataTemplate> 
  2. And a TreeView like this 还有像这样的TreeView

     <TreeView Name="TreeViewFileTree" ItemsSource="{rootFolder.Children}" /> 

That's it. 而已。 WPF's strength is its simplicity. WPF的优势在于其简洁性。

You need to use You'll need 3 things: 你需要使用你需要3件事:

  1. a HierarchicalDataTemplate, like you have, to do parent+children, and template the folders. 像你一样使用HierarchicalDataTemplate来做父/子,并对文件夹进行模板化。 you MIGHT be able to use a CompositeCollection here to merge the folders+files, but i'm not sure about that...you might have to add another property to your folder class that returns the union of files and folders and call it "Children" or whatever... 你可以在这里使用CompositeCollection来合并文件夹+文件,但我不确定...你可能需要在你的文件夹类中添加另一个属性来返回文件和文件夹的联合并调用它“孩子们“或者其他......
  2. A DataTemplate to template files in the tree DataTemplate到树中的模板文件
  3. A TemplateSelector to tell the tree to switch between templates depending on the item in the tree. 一个TemplateSelector,用于告诉树在模板之间切换,具体取决于树中的项目。 Instead of setting an ItemTemplate on the tree, set the ItemTemplateSelector to this. 不要在树上设置ItemTemplate,而是将ItemTemplateSelector设置为this。

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

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