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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:29:19+00:00 2026-05-30T10:29:19+00:00

I have class Person: public class Person : INotifyPropertyChanged { private String name =

  • 0

I have class Person:

    public class Person : INotifyPropertyChanged
    {

        private String name = "";
        public event PropertyChangedEventHandler PropertyChanged;

        public Person()
        {
            NameProperty = "hi";
        }

        public Person(String _name)
        {
            NameProperty = _name;
        }

        public String NameProperty
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("NameProperty");
            }
        }

        public override String ToString()
        {
            return NameProperty;
        }

        protected void OnPropertyChanged(string name)
        {

            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }

        }

    }

And I can bind the instance of my class to TextBox:

myPerson = new Person("demas");
Binding myBinding = new Binding("NameProperty");
myBinding.Source = myPerson;
txtName.SetBinding(TextBox.TextProperty, myBinding);

Now I have collection of my class and want to bind it to ListBox:

List<Person> myCollection = new List<Person>(); // on the MainWindow level

myCollection.Add(new Person("one"));
myCollection.Add(new Person("two"));
myCollection.Add(new Person("three"));

So I have to see in the ListBox three values: one, two, three. How can I do it?

  • 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-30T10:29:20+00:00Added an answer on May 30, 2026 at 10:29 am

    Expose your collection as a public property in your MainWindow. If you want your ListBox to get updated when elements are added or removed to your collection, you should use an ObservableCollection<T> rather than List<T>.

    readonly ObservableCollection<Person> myCollection = new ObservableCollection<Person>();
    
    public ObservableCollection<Person> MyCollection
    {
        get { return myCollection; }
    } 
    

    Then, in your XAML, use data binding to reference the property:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:l="clr-namespace:WpfApplication1"
            xmlns:p="clr-namespace:WpfApplication1.Properties"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ListBox ItemsSource="{Binding MyCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding NameProperty}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Window>
    

    Don’t forget to set the DataContext either on your ListBox or on its parent window.

    P.S. Identifier names should identify the purpose, not the type. Use Persons instead of MyCollection; use Name instead of NameProperty.

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

Sidebar

Related Questions

I have : public class Person : INotifyPropertyChanged { private string _name; public int
I have a class : public class Person { public string name { get;
I have a class like this: class person { public string Name{get;set;} public byte[]
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have a class called Person: public class Person { private String name,
i have class like below class Person { public string Name{get;set;} public string Surname{get;set;}
Example-I have a person class Public Class Person Private _fname As String Public Property
I have a class like this, public class person { name Name {set; get;}
I have the following class Person: public class Person { public string Name {
I have a class public class Person { public string FirstName { get; set;

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.