title: Using ItemsSource to populate WPF ListBox - Good Idea?
body: <p>I'm a (relatively) experienced Cocoa/Objective-C coder, and am teaching myself C# and the WPF framework.</p>
  
  <p>In Cocoa, when populating an <code>NSTableView</code>, it's relatively simply to assign a delegate and datasource to the view. Those delegate/datasource methods are then used to populate the table, and to determine its behavior.</p>
  
  <p>I'm putting together a simple application that has a list of objects, lets call them <code>Dog</code> objects, that each have a <code>public string name</code>. This is the return value of <code>Dog.ToString()</code>.</p>
  
  <p>The objects will be displayed in a <code>ListBox</code>, and I would like to populate this view using a similar pattern to Cocoa's <code>NSTableViewDataSource</code>. It currently seems to be working using:</p>
  
  <pre><code>public partial class MainWindow : Window, IEnumerable&lt;Dog&gt;
      {
          public Pound pound = new Pound();
  
          public MainWindow()
          {
              InitializeComponent();
  
              Dog fido = new Dog();
              fido.name = "Fido";
              pound.AddDog(fido);
  
              listBox1.ItemsSource = this;
  
              Dog spot = new Dog();
              spot.name = "Spot";
              pound.AddDog(spot);
          }
  
          public IEnumerator&lt;Dog&gt; GetEnumerator()
          {
              return currentContext.subjects.GetEnumerator();
          }
  
          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
          {
              return GetEnumerator();
          }
      }
  </code></pre>
  
  <p>But I'm wondering how <em>correct</em> this is. I've literally had Visual Studio installed for less than an hour, so it's safe to say I have no idea what I'm doing.</p>
  
  <ol>
  <li>Is this the proper pattern?</li>
  <li>Adding the second item to the list (<code>spot</code>) seems to update the <code>ListBox</code> properly, but I'm wondering what triggers the updates?</li>
  <li>What happens if I update the <code>Pound</code> on a background thread?</li>
  <li>How can I manually ask the <code>ListBox</code> to update itself? (Do I even need to?)</li>
  </ol>
  
  <p><strong>One change</strong> that I know I need to make is refactoring the <code>IEnumerable&lt;Dog&gt;</code> implementation into its own class, like <code>DogListItemsSource</code>, but I want to make sure I have a solid approach before polishing it.</p>
  
  <p><strong>Feel free</strong> to point out, in comments, any other points I should address or keep in mind, big or small. I'd like to learn this the right way, the first time.</p>