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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:41:10+00:00 2026-05-15T02:41:10+00:00

I have a simple class as defined below: public class Person { int _id;

  • 0

I have a simple class as defined below:

public class Person
{
    int _id;
    string _name;

    public Person()
    { }

    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

that is stored in a database, and thru a bit more code I put it into an ObservableCollection object to attempt to databind in WPF later on:

 public class People : ObservableCollection<Person>
{
    public People() : base() { }

    public void Add(List<Person> pListOfPeople)
    {
        foreach (Person p in pListOfPeople) this.Add(p);
    }
}

In XAML, I have myself a ListView that I would like to populate a ListViewItem (consisting of a textblock) for each item in the "People" object as it gets updated from the database. I would also like that textblock to bind to the "Name" property of the Person object.

I thought at first that I could do this:

lstPeople.DataContext = objPeople;

where lstPeople is my ListView control in my XAML, but that of course does nothing. I’ve found TONS of examples online where people through XAML create an object and then bind to it through their XAML; but not one where we bind to an instantiated object and re-draw accordingly.

Could someone please give me a few pointers on:

A) How to bind a ListView control to my instantiated "People" collection object?

B) How might I apply a template to my ListView to format it for the objects in the collection?

Even links to a decent example (not one operating on an object declared in XAML please) would be appreciated.

  • 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-15T02:41:11+00:00Added an answer on May 15, 2026 at 2:41 am

    You were so close.

    lstPeople.ItemsSource = objPeople; // :)
    

    The only other thing you need is how to apply a view for each item in your collection. No problem. I won’t use a ListView… I’ll just use an ItemsControl because it’s a bit simpler.

    <ItemsControl x:Name="lstPeople">
        <ItemsControl.ItemTemplate>
             <DataTemplate>
                  <TextBlock Text="{Binding Name}" />
             </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    That’s pretty much it. The strategy is the same for a Listview, but you need to provide just a tad more XAML to provide column headers and stuff. I’m not 100% sure you need this at the moment, so I left it out.

    Edit: Here’s an extension method for “AddRange” that will do what you are trying to do by subclassing ObservableCollection. Little easier… especially if you end up with a lot of collection types (you will)

    public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)
    {
         foreach(var item in items)
         {
              collection.Add(item);
         }
    }
    

    Then you can just do:

    ObservableCollection<Person> peeps = new ObservableCollection<Person>();
    peeps.AddRange(new List<Person>
    { 
         new Person() { Name = "Greg" }, 
         new Person() { Name = "Joe" } 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.