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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:07:03+00:00 2026-06-17T08:07:03+00:00

I bound (at least i think i did) data to a ListBox following a

  • 0

I bound (at least i think i did) data to a ListBox following a tutorial. The class element I want bound has data in it but i see nothing on the ListBox after some event. I have the following partial XAML:

 <ListBox x:Name="jukeBoxListBox" Height="227" VerticalAlignment="Top" ItemsSource="{Binding FilePathList}"/>

In the WPF form cs file I have. Should i set to class FolderItems or its attr filePathList? Also should I use ObservableCollection instead of list?

InitializeComponent();    
FolderItems folderItems = new FolderItems();
this.DataContext = folderItems.FilePathList;

My data class:

class FolderItems : INotifyPropertyChanged
{
    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected void Notify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion INotifyPropertyChanged implementation

    private ObservableCollection<String> _pathList = new ObservableCollection<string>();

    public ObservableCollection<String> FilePathList
    {
        get { return _pathList; }
        set
        {
            if (value != _pathList)
            {
                _pathList = value;
                Notify("FilePathList");
            }
        }
    }
}

I think I need to mention that I change the List elements in a Button click event. Maybe the below is a part of the problem.

//in the event fItems is an instance of FolderItems   
var files = new ObservableCollection<string>();
ProcessFiles(of.SelectedPath, files);
fItems.FilePathList = files;
//...

    private void ProcessFiles(string path, ICollection<string> files)
    {
        foreach (var file in Directory.GetFiles(path).Where(name => name.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)))
            files.Add(file);    

        foreach (var directory in Directory.GetDirectories(path))
            ProcessFiles(directory, files);

    }

I hail from Javaland and am brand new to C#. Please excuse my language.

  • 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-17T08:07:04+00:00Added an answer on June 17, 2026 at 8:07 am

    If you’d change List<string> to ObservableCollection<string> (see here), your binding would get notified about changes in the list, e.g. when you add items.

    Also, you must change the property name in the Notify call to filePathList.

    And you should follow coding conventions for properties in .Net, which are usually written with a leading uppercase character. So your property would be FilePathList.

    private ObservableCollection<String> pathList = new ObservableCollection<string>();
    
    public ObservableCollection<String> FilePathList
    {
        get { return pathList; }
        set
        {
            if (value != pathList)
            {
                pathList = value;
                Notify("FilePathList"); // changed here
            }
        }
    }
    

    Change the binding to the renamed property:

    <ListBox ... ItemsSource="{Binding FilePathList}"/>
    

    See also Binding to Collections and Using Collection Objects as a Binding Source.


    UPDATE

    Your ProcessFiles method should be written as shown below to enable recursion.

    private void ProcessFiles(string path, ICollection<string> files)
    {
        foreach (var file in Directory.GetFiles(path).Where(name => name.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)))
        {
            files.Add(file);
        }
    
        foreach (var directory in Directory.GetDirectories(path))
        {
            ProcessFiles(directory, files);
        }
    }
    

    And be called like this:

    var files = new ObservableCollection<string>();
    ProcessFiles(of.SelectedPath, files);
    
    var folderItems = new FolderItems();
    folderItems.FilePathList = files;
    DataContext = folderItems;
    

    Or if you need to access the FolderItems object later (perhaps in some event handler) you might get it back from the DataContext:

    DataContext = new FolderItems();
    
    ...
    
    var folderItems = DataContext as FolderItems;
    ProcessFiles(of.SelectedPath, folderItems.FilePathList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data bound listbox that lists names of items, I am now
I want to bound google map in android around the UK,so the user can't
I have a DataGridView bound to a LINQ to SQL query expression. I want
I have a ListBox bound to a list of Items (for arguement, lets say
I want to divide the elements of my array for the first element of
I have an array of keywords, and I want to know whether at least
I've seen code examples of divide-and-conquer algorithms (or, at least what I think are
I think I must be missing something obvious, but I'm unable to find this
I have the following problem. I want to write a method with which i
I did search other post but to no avail. With C-x C-k n NameOfMacro

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.