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 8859519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:04:56+00:00 2026-06-14T15:04:56+00:00

I want to view a folder structure in a treeview using a databinding. The

  • 0

I want to view a folder structure in a treeview using a databinding.
The folder class just has a property list of children and a property name.

If something changes it will fire the according event.
This is it:

    public class Folder : INotifyPropertyChanged, INotifyCollectionChanged
    {    
        public event PropertyChangedEventHandler PropertyChanged;           
        public event NotifyCollectionChangedEventHandler CollectionChanged; 

        public Folder(string name)
        {
            this.Name = name;
            this.ContentFolders = new List<Folder>();
        }

        public List<Folder> ContentFolders { get; set; }

        public void AddFolder(Folder f)
        {
            this.ContentFolders.Add(f);

            if (this.CollectionChanged != null)
            {
                this.NotifyCollectionChanged(
                      new NotifyCollectionChangedEventArgs(
                          NotifyCollectionChangedAction.Add, f));
            }
            this.PropertyChanged(this, new PropertyChangedEventArgs("ContentFolders"));
        }

        private void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            lock (CollectionChanged)
            {
                if (CollectionChanged != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => CollectionChanged(this, e)));
                }
            }
        }

        private string name;

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(
                            this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }

    }

This is my GUI, which shows the root folder in a treeview:

    <Window x:Class="WpfApplication2.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication2="clr-namespace:WpfApplication2"
            Title="MyWindow" Height="300" Width="300" xmlns:u="clr-namespace:UpdateControls.XAML;assembly=UpdateControls.XAML">
      <StackPanel>
        <StackPanel.Resources>
          <HierarchicalDataTemplate DataType="{x:Type WpfApplication2:Folder}"
                                    ItemsSource="{Binding Path=ContentFolders}">
            <TextBlock Text="{Binding Path=Name}" />
          </HierarchicalDataTemplate>
        </StackPanel.Resources>
        <TreeView Name="TreeviewScenario">
          <TreeViewItem Header="{Binding Path=RootFolder.Name}"
                        ItemsSource="{Binding Path=RootFolder.ContentFolders}" />
        </TreeView>
        <Button Content="Add Folder" Click="Button_Click" />
      </StackPanel>
    </Window>

The according MyWindow.xaml.cs class has a property Folder and adds some content. It has also a method for the button to add a new folder if it becomes clicked.

public partial class MyWindow : Window
{
    public Folder RootFolder { get; set; }

    public MyWindow()
    {
        this.RootFolder = new Folder("root");
        this.RootFolder.ContentFolders.Add(new Folder("1"));
        this.RootFolder.ContentFolders.Add(new Folder("12"));
        this.RootFolder.ContentFolders.Add(new Folder("13"));
        this.RootFolder.ContentFolders.Add(new Folder("14"));
        this.RootFolder.ContentFolders.Add(new Folder("15"));

        Folder aFolder = new Folder("aFolder");
        aFolder.ContentFolders.Add(new Folder("2"));
        aFolder.ContentFolders.Add(new Folder("21"));
        aFolder.ContentFolders.Add(new Folder("22"));
        aFolder.ContentFolders.Add(new Folder("23"));
        aFolder.ContentFolders.Add(new Folder("24"));

        this.RootFolder.ContentFolders.Add(aFolder);

        this.DataContext = this;
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Folder c = new Folder("a new Folder");
        this.RootFolder.AddFolder(c);
    }
}

The Gui will be calles by a simple Main method with:

    new MyWindow();

If I start, the treeview looks fine, it has all the items, which have been added in the MyWindow.xaml.cs.

But If I click the button, no new items will be shown. If I click the button before I expand the treeview, the new items will be there…

So the view seems not to be updated…

Can anybody see, what I have done wrong?

  • 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-14T15:04:57+00:00Added an answer on June 14, 2026 at 3:04 pm

    Change the ContentFolders in your Folder class to an ObservableCollection<> instead of a List<>

        public ObservableCollection<Folder> ContentFolders { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Crystal report 7 I want to view the table 1 and sum of
Using NERDTree plugin , I want to view only *.txt files. There is a
i have just started playing aorund with asp.net mvc and i want to view
I want to be able to change dynamically the view folder. The aim is
I'm currently developing a site using ASP.Net MVC3 with Razor. Inside the View/Shared folder,
I am using Sugar Pro 6.4 and inside a customized view I want to
I have the following folder structure in python: houses/ models.py __init__.py view/ houses.py events.py
I have two views, both under the NavigationController. I don't want View 1 (on
I want to view the stream of the RGB data color stream on an
i want a view with UITabBAr at The bottom with some buttons+Navigation controller on

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.