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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:11:11+00:00 2026-05-27T07:11:11+00:00

This question looks like this one : Programmatically binding List to ListBox but as

  • 0

This question looks like this one :
Programmatically binding List to ListBox
but as far as my example is concerned, the answers given in this post doesn’t work for me !
Anyway, I looked at about 10/15 similar posts but none worked

Here is my xaml code :

<Grid>
    <ListBox Name="ListBoxx"/>
</Grid>

And here is the code behind :

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ObservableCollection<string> MyCollection = new ObservableCollection<string>();

            Random random = new Random();
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += (sender, e) =>
            {
                MyCollection.Add(random.Next(0, 100).ToString());
            };
            aTimer.Interval = 500;
            aTimer.Enabled = true;

            Binding myBinding2 = new Binding();
            myBinding2.Source = this;
            myBinding2.Path = new PropertyPath("MyCollection");
            ListBoxx.SetBinding(ListBox.ItemsSourceProperty, myBinding);
        }
    }
}

As far as I know, my class doesn’t have to inherit from INotifyPropertyChanged cause I use an ObservableCollection.

I don’t wanna use a {Binding} in xaml code.

I tried this code :

ListBoxx.ItemsSource = MyCollection;

this “works” but I got a thread crash. I know I can use Dispatcher to fix it but it doesn’t fix with my code. I wanna use a “real” binding (configurable) like in my code snippet.

The result is that I got no error but the ListBox just doesn’t update :/

I tried to add the lines :

MyCollection.Add("aaa");
MyCollection.Add("bbb");
MyCollection.Add("ccc");
MyCollection.Add("ddd");

but I don’t get anything in my application so I really think this is a binding problem. Anyway, I’m SURE that my Timer is OK, The problem’s not here.

I tried too to get MyCollection as Property like this :

private ObservableCollection<string> _MyCollection = new ObservableCollection<string>();
public ObservableCollection<string> MyCollection { get { return _MyCollection; } }

but it doesn’t work too :/ (no error but no update)

  • 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-27T07:11:12+00:00Added an answer on May 27, 2026 at 7:11 am

    The code you posted is going to fail because “MyCollection” isn’t a property in MainWindow. Your Visual Studio output window is probably showing binding errors.

    Additionally you indeed need to dispatch the changes you make to the collection, you can use a DispatcherTimer to do this. There should be errors in the output window about that too.

    The code below works:

    public partial class MainWindow : Window
    {
        public ObservableCollection<string> MyCollection { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
            MyCollection = new ObservableCollection<string>();
    
            Random random = new Random();
            DispatcherTimer aTimer = new DispatcherTimer();
            aTimer.Tick += (sender, e) =>
            {
                MyCollection.Add(random.Next(0, 100).ToString());
            };
            aTimer.Interval = new TimeSpan(0,0,0,0,500);
            aTimer.Start();
    
            Binding myBinding2 = new Binding();
            myBinding2.Source = this;
            myBinding2.Path = new PropertyPath("MyCollection");
            ListBoxx.SetBinding(ListBox.ItemsSourceProperty, myBinding2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just saw this question and one of the answers looks really appealing to
Okay guess this question looks a lot like: What is the best way to
Absolute beginner question: I have a template file index.html that looks like this: ...
Quick question... I have a query that checks for duplicates that looks like this:
This might look like a basic question to some of you but I expect
This sounds like a look-up-in-the-manual question to me, but I can't find it. Suppose
I'm probably going to take some heat for this question. But I'd like to
This question probably looks a lot like IDE for ironpython on windows question here
Expanding on this question , it looks like I did not provide enough detail.
It looks like this question has been asked before, here and in other places

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.