question

abhijeetkhopade-1471 avatar image
0 Votes"
abhijeetkhopade-1471 asked ·

WPF Treeview Binding

Hi Experts,
I would like to populate WPF Treeview with Directory and subdirectories. For that I followed this https://social.msdn.microsoft.com/Forums/vstudio/en-US/bdc74177-0db3-4e9d-9a08-96406eb981a8/wpf-treeview-with-checkboxes?forum=wpf link.
My confusion is how to add Parent Directory and child (sub) directory in this model and populate the treeview.

Can some one please help me here?

windows-wpf
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There are multiple types of sample to bind data to treeview, which one did you follow? How do you want to populate the Treeview with the Parent/child directory?

0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

Hi, try following demo:

 <Window x:Class="WpfApp1.Window48"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:WpfApp48"
         mc:Ignorable="d"
         Title="Window48" Height="450" Width="800">
   <Window.DataContext>
     <local:ViewModel/>
   </Window.DataContext>
   <Grid>
     <TreeView ItemsSource="{Binding View}">
       <TreeView.Resources>
         <HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding SubFolders}">
           <TextBlock Text="{Binding Name}"/>
         </HierarchicalDataTemplate>
       </TreeView.Resources>
     </TreeView>
   </Grid>
 </Window>


 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.IO;
 using System.Windows;
 using System.Windows.Data;
 using IO = System.IO;
    
 namespace WpfApp48
 {
   public class ViewModel
   {
     public ViewModel()
     {
       string path = @"c:\temp";
       foreach (var dir in Directory.GetDirectories(path)) LoadFolder(dir, col);
       cvs.Source = col;
     }
    
     private void LoadFolder(string path, ObservableCollection<Folder> col)
     {
       Folder folder = new Folder() { Name = IO.Path.GetFileNameWithoutExtension(path) };
       col.Add(folder);
       foreach (var dir in Directory.GetDirectories(path)) LoadFolder(dir, folder.SubFolders);
     }
     public ICollectionView View { get => cvs.View; }
     private CollectionViewSource cvs = new CollectionViewSource();
     private ObservableCollection<Folder> col = new ObservableCollection<Folder>();
   }
    
   public class Folder
   {
     public string Name { get; set; }
     public ObservableCollection<Folder> SubFolders { get; set; } = new ObservableCollection<Folder>();
   }
 }
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Peter,
Thank you very much for your answer, thats perfect thats what I was looking for.

Thanks again,

0 Votes 0 ·