WPF Treeview Binding

abhijeet khopade 21 Reputation points
2020-06-17T21:54:43.123+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,679 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-06-18T05:23:58.39+00:00

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

0 additional answers

Sort by: Most helpful