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

  • Home
  • SEARCH
  • 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 8474117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:32:22+00:00 2026-06-10T17:32:22+00:00

I’m relatively new to MVVM and WPF. I’m attempting to fill a TreeView control

  • 0

I’m relatively new to MVVM and WPF. I’m attempting to fill a TreeView control with a directory and it’s files / subdirectories (in effect the contents of a zip file that I have unpacked)

Following along after this SO question, I have the following class:

namespace IFR_Full.Model
{
    public class Item
    {
        public string Name { get; set; }
        public string Path { get; set; }
    }

    public class FileItem : Item
    {
    }

    public class DirectoryItem : Item
    {
        public List<Item> Items { get; set; }

        public DirectoryItem()
        {
            Items = new List<Item>();
        }
    }

    public class TVItemProvider
    {
        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;
        }
    }
}

In my ViewModel class I have these properties:

TVItemProvider TVIP = new TVItemProvider();

private List<Item> _tvitems;
public List<Item> TVItems
{
    get { return _tvitems; }
}

which is created in this method:

private void LoadIDMLTreeView(string path)
{
    _tvitems = TVIP.GetItems(path);
}

I set the header and DataContext of my MainWindow like this:

...
xmlns:ViewModel="clr-namespace:IFR_Full"
xmlns:Model ="clr-namespace:IFR_Full.Model"
...

<Window.DataContext>
    <ViewModel:ExcelImportViewModel/>
</Window.DataContext>

and set my treeview xaml code like this:

<TreeView ItemsSource="{Binding}" Name="IDMLView" Margin="10,171.74,10,8" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}" ItemsSource="{Binding Path=TVItems}">
            <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>             
 </TreeView.Resources>
</TreeView>

When I run the program in debug mode I can see that TVItems contains the appropriate items (Directories and files), but my TreeView control is blank.

I imagine that the issue is with the bindings?

  • 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-06-10T17:32:24+00:00Added an answer on June 10, 2026 at 5:32 pm
    • Change <TreeView ItemsSource="{Binding}" ... to <TreeView ItemsSource="{Binding TVItems}" ...
    • Also , Change to <HierarchicalDataTemplate DataType="{x:Type local:DirectoryItem}" ItemsSource="{Binding Items}" >
    • Your class has to be like this :

       public class TVItemProvider
          {
      
              List<object> items = new List<object>();
                DirectoryInfo dirInfo;
                public List<object> GetItems(string path)
              {
                  dirInfo = new DirectoryInfo(path);
                  foreach (var directory in dirInfo.GetDirectories())
                  {
                      var item = new DirectoryItem
                      {
                          Name = directory.Name,
                          Path = directory.FullName,
                          Items = new TVItemProvider().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;
              }
      

      }

    • Finally change the type of your lists to List<object> (all of them)

      Hope it would help

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.