Treeview multibinding in wpf

I want to bind a tree structure to a class like this:

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; } } 

other ares classes:

 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 to create a tree structure that displays a collection of files and folders

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 as follows:

Rootfolder

 | |-File |-File |-Folder |-File |-File |-Folder |-File 
+6
c # wpf multibinding
source share
3 answers

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

EDIT: as mentioned in the comments, the Intuipic application does something very similar to what you are requesting. Here is a screenshot:

alt text

+2
source share

This is pretty easy considering your constellation.

First: set up your classes. You do not need two separate lists for files and folders in the folder class. Just use one IList<Base_FileFolder> inside the Base_FileFolder class (good OOP) and name it Children!

Then you will need two more steps:

  • Two hierarchical patterns

     <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> 
  • And a treeview like this

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

What is it. The strength of WPF is its simplicity.

+1
source share

You need to use You will need 3 things:

  • Hierarchical DataTemplate, like you, for creating parent + child elements and folder templates. you MAY be able to use CompositeCollection here to combine folders + files, but I'm not sure about that ... you might have to add another property to your folder class that returns the file and folder combination and calls it “Children” or what something else ...
  • DataTemplate for template files in the tree
  • Template Selector to tell the tree to switch between templates depending on the item in the tree. Instead of setting the ItemTemplate in the tree, set it to ItemTemplateSelector.
0
source share

All Articles