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

Is there any website where people share and discuss good examples of object-oriented design?
I have a structure which I need to populate and write to disk (several
There are several questions on StackOverflow discussing the question of when one should use
I am using iText and am very new to it. There have been several
I've read several different posts on paging w/ in MVC but none describe a
For most database-backed projects I've worked on, there is a need to get startup
I'm not a beginner, but the more I think about this issue, the more
Note: By workflow i'm not referring to workflow technology, such as Workflow foundation. All
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I'm reading Apress Pro ASP.NET MVC Framework and there are some LINQ to SQL

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.