views:

64

answers:

3

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
+1  A: 

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

HTH,
Kent

Kent Boogaart
Read it again please I add some text thx
Delta
Intuipic (http://intuipic.codeplex.com/) is a project I wrote a while back that does this. You might want to check it out.
Kent Boogaart
@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.
John Gardner
@John: if you cared to actually look, you'll see that it displays a folder and file tree view exactly like the OP requested.
Kent Boogaart
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"
John Gardner
@John: you may not be motivated to look, but I trust that the OP is.
Kent Boogaart
I've added a screenshot.
Kent Boogaart
The screenshot doesn't show files and folders at the same level of the tree.
Joe White
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.
Kent Boogaart
A: 

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.
John Gardner
+1  A: 

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.

Falcon