treeview Multibinding in wpf
Asked Answered
U

3

6

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:

rootFolder

|
|-File
|-File
|-Folder
  |-File
  |-File
  |-Folder
    |-File
Unleavened answered 20/8, 2010 at 16:37 Comment(1)
This is the exact same issue I ran into when I wanted to fill a treeview with every attribute of every element in an XDocument, I could iterate over the attributes of one element, or all the elements, but iterating all elements and all attributes on each element threw me for a loop.. Don't recall what I did, but it wasted a lot of my time.Pastiness
I
2

What exactly is your question? How to combine them? CompositeCollection.

EDIT: as mentioned in the comments, my Intuipic application does something very similar to what you're requesting. Here's a screenshot:

alt text

Incretion answered 20/8, 2010 at 17:45 Comment(7)
Intuipic (intuipic.codeplex.com) is a project I wrote a while back that does this. You might want to check it out.Incretion
@KenBoogaart, really? "Intuipic is an easy to use image viewer. It aims to be intuitive to use, and functional at the same time. " that doesn't sound like it does anything like the question.Alyciaalyda
@John: if you cared to actually look, you'll see that it displays a folder and file tree view exactly like the OP requested.Incretion
I cared to look, i just wasn't going to spend the time to run a clickonce installer or browse a codeplex source tree to see what may or may not have been anything useful. There's nothing on the codeplex site in any screenshots or anything i could find that showed a tree view like the poster was asking for. Your original note would have been more helpful if you'd said "You might want to check out file x\y\z in that project to see. how i did it"Alyciaalyda
@John: you may not be motivated to look, but I trust that the OP is.Incretion
The screenshot doesn't show files and folders at the same level of the tree.Sigismond
There's no need to nit-pick. I had forgotten exactly what it did because it's been 3 years since I wrote it. What it does do, is show a heterogeneous collection of items bound hierarchically to a TreeView. I have faith that the OP can extrapolate this to encompass files, too.Incretion
D
1

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!

Then you'll need only two more steps:

  1. Two 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 Name="TreeViewFileTree" ItemsSource="{rootFolder.Children}" />
    

That's it. WPF's strength is its simplicity.

Devitrify answered 6/9, 2010 at 12:21 Comment(0)
A
0

You need to use You'll need 3 things:

  1. a HierarchicalDataTemplate, like you have, to do parent+children, and template the folders. 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...
  2. A DataTemplate to template files in the tree
  3. A TemplateSelector to tell the tree to switch between templates depending on the item in the tree. Instead of setting an ItemTemplate on the tree, set the ItemTemplateSelector to this.
Alyciaalyda answered 20/8, 2010 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.