Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6103769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:46:00+00:00 2026-05-23T13:46:00+00:00

There are several examples of how to populate a tree view from a collection

  • 0

There are several examples of how to populate a tree view from a collection of file paths such as this or this other example. I cannot seem to find such example for WPF. I know I can integrate windows forms and use a different control in order to make it work but it will be nice if I could do the same thing with a wpf treeview control. The tree view that I want to construct consists of about 50,000 files therefore I think it will be better if it is bind it to something. But first before binding it, I think it will be helpful to construct one based on a List of strings (strings contains the paths of files).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:46:00+00:00Added an answer on May 23, 2026 at 1:46 pm

    I was intrigued by the question and threw this together. As a first pass I think I’m pretty close to what you’re looking for. Talking about 50,000 items though makes me think that lazy loading may be appropriate. Anyway, here is the simple version based on an article by Josh Smith. I put all of the code here, but the magic really takes place with the data templates.

    Given a few classes to represent the objects we’re working with…

    using System.Collections.Generic;
    
    namespace WpfTreeViewBinding.Model
    {
        public class Item
        {
            public string Name { get; set; }
            public string Path { get; set; }
        }
    }
    

    and…

    namespace WpfTreeViewBinding.Model
    {
        public class FileItem : Item
        {
    
        }
    }
    

    and…

    namespace WpfTreeViewBinding.Model
    {
        public class DirectoryItem : Item
        {
            public List<Item> Items { get; set; }
    
            public DirectoryItem()
            {
                Items = new List<Item>();
            }
        }
    }
    

    I created a recursive method to load up some directories/files…

    using System.Collections.Generic;
    using System.IO;
    using WpfTreeViewBinding.Model;
    
    namespace WpfTreeViewBinding
    {
        public class ItemProvider
        {
            public List<Item> GetItems(string path)
            {
                var items = new List<Item>();
    
                var dirInfo = new DirectoryInfo(path);
    
                foreach(var directory in dirInfo.GetDirectories())
                {
                    var item = new DirectoryItem
                                   {
                                       Name = directory.Name,
                                       Path = directory.FullName,
                                       Items = GetItems(directory.FullName)
                                   };
    
                    items.Add(item);
                }
    
                foreach(var file in dirInfo.GetFiles())
                {
                    var item = new FileItem
                                   {
                                       Name = file.Name, 
                                       Path = file.FullName
                                   };
    
                    items.Add(item);
                }
    
                return items;
            }
        }
    }
    

    From there it’s just a matter of getting the data…

    using System.Windows;
    
    namespace WpfTreeViewBinding
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var itemProvider = new ItemProvider();
    
                var items = itemProvider.GetItems("C:\\Temp");
    
                DataContext = items;
            }
        }
    }
    

    And displaying it…

    <Window x:Class="WpfTreeViewBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:Model="clr-namespace:WpfTreeViewBinding.Model" 
            Title="MainWindow" 
            Height="350" Width="525">
    
        <Window.Resources>
    
            <HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}"
                                      ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
            </HierarchicalDataTemplate>
    
            <DataTemplate DataType="{x:Type Model:FileItem}">
                <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
            </DataTemplate>
    
        </Window.Resources>
    
        <Grid Margin="8">
            <TreeView ItemsSource="{Binding}" />
        </Grid>
    
    </Window>
    

    All of the magic really happens with the data templates. I guess the key to the whole thing is using the HierarchicalDataTemplate for any items with hierarchy (i.e. directories).

    NOTE 1: I haven’t extensively tested this. It hasn’t been profiled for performance. I would welcome any feedback though since this is a problem I tried to solve long ago and gave up on. Thanks!

    NOTE 2: You’ll need to set the hard-coded path to something that makes sense on your system.

    Here is a screenshot showing directories and files at different levels…

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are several other questions about this topic that I have gone through, but
I am trying to understand how to use reference parameters. There are several examples
There have been several questions posted to SO about floating-point representation. For example, the
There are several threads on this here at SO but I didn't find one
There are several variant questions on this on SO, but I did not find
There are several ways google throws at me for checking if a file is
There are several extensive code examples on a tutorial page at the ember web
Examples: Say I have several 1D arrays, such as A, B, and C: A
I've tried answers from other similar stackoverflow questions, but cannot figure out what i
I know there are several examples of how to animate a storyboard when they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.