title: WPF Databinding With A Collection Object
body: <p>I have a simple class as defined below:</p>
  <pre><code>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; }
      }
  }
  </code></pre>
  <p>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:</p>
  <pre><code> public class People : ObservableCollection&lt;Person&gt;
  {
      public People() : base() { }
  
      public void Add(List&lt;Person&gt; pListOfPeople)
      {
          foreach (Person p in pListOfPeople) this.Add(p);
      }
  }
  </code></pre>
  <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 &quot;People&quot; object as it gets updated from the database. I would also like that textblock to bind to the &quot;Name&quot; property of the Person object.</p>
  <p>I thought at first that I could do this:</p>
  <pre><code>lstPeople.DataContext = objPeople;
  </code></pre>
  <p>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.</p>
  <p>Could someone please give me a few pointers on:</p>
  <p>A) How to bind a ListView control to my instantiated &quot;People&quot; collection object?</p>
  <p>B) How might I apply a template to my ListView to format it for the objects in the collection?</p>
  <p>Even links to a decent example (not one operating on an object declared in XAML please) would be appreciated.</p>