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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:40:59+00:00 2026-05-29T22:40:59+00:00

I am new to WPF. I have created a WPF project, and add the

  • 0

I am new to WPF. I have created a WPF project, and add the following class

public class MessageList:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    private List<string> list = new List<string>();

    public List<string> MsgList
    {
        get { return list; }
        set
        {
            list = value;
            OnPropertyChanged("MsgList");
        }
    }

    public void AddItem(string item)
    {
        this.MsgList.Add(item);

        OnPropertyChanged("MsgList");
    }
}

Then in the main window I added a ListBox and below is the xaml content

<Window.DataContext>
        <ObjectDataProvider x:Name="dataSource" ObjectType="{x:Type src:MessageList}"/>
    </Window.DataContext>
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="52,44,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <ListBox Height="233" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Margin="185,44,0,0" Name="listBox1" VerticalAlignment="Top" Width="260" ItemsSource="{Binding Path=MsgList}" />
    </Grid>

Here is the source code of MainWindow.cs

public partial class MainWindow : Window
    {
        private MessageList mlist = null;

        public MainWindow()
        {
            InitializeComponent();
            object obj = this.DataContext;
            if (obj is ObjectDataProvider)
            {
                this.mlist = ((ObjectDataProvider)obj).ObjectInstance as MessageList;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.mlist.AddItem(DateTime.Now.ToString());
        }
    }

My question is after I clicked the button, there isn’t any content displayed on the Listbox, what is the reason?

  • 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-29T22:41:00+00:00Added an answer on May 29, 2026 at 10:41 pm

    You asked for a reason, while devdigital gave you the solution its worth mentioning why it is not working, and why his fix works:

    Your mlist is bound to the ListBox and its all working well. Now you press the button and you add an entry to your list. The listbox just won’t know about this change, because your list has no way of telling “Hey i just added a new item”. To do that, you need to use a Collection implementing INotifyCollectionChanged, like the ObservableCollection. This is very similar to your OnPropertyChanged if you modify a property on your MessageList it also calls the OnPropertychanged method which fires the PropertyChanged event. The Databinding registers to the PropertyChanged event and now knows when you updated your property and automatically updates the UI. The same is necessary for Collections if you want this automatic updating of the UI on collections.

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

Sidebar

Related Questions

I am new to WPF. I have Created some user Controls with some basic
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I have a class library that I created using the Class Library project template.
I'm new to WPF. I have a WPF Window with a bunch of Labels
I'm kind of new to WPF although I have some experience in Forms, and
I have a WPF sorting/binding issue. (Disclaimer: I am very new to WPF and
New to wpf and through a learning curve. I have a userControl with a
I am new to WPF. I have a ListBox that has its ItemSource set
I am new to WPF. I have a code which displays a UI window
I am new to wpf and i am working on first application i have

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.