title: Binding a ListBox's SelectedItem in the presence of BindingNavigator
body: <p>I'm trying to bind a ListBox's SelectedItem data to a property. The following code is an example:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Windows.Forms;
  
  namespace BindingFailure
  {
   static class Program
   {
    class OuterObject
    {
     public string selected { get; set; }
     public List&lt;string&gt; strings { get; set; }
    }
  
    public static void Main()
    {
     List&lt;OuterObject&gt; objs = new List&lt;OuterObject&gt;()
     {
      new OuterObject(), new OuterObject()
     };
     objs[0].strings = new List&lt;string&gt; { "one", "two", "three" };
     objs[1].strings = new List&lt;string&gt; { "four", "five", "six" };
  
     Form form = new Form();
  
     BindingSource obs = new BindingSource(objs, null),
      ibs = new BindingSource(obs, "strings");
     BindingNavigator nav = new BindingNavigator(obs);
     ListBox lbox = new ListBox();
     lbox.DataSource = ibs;
     lbox.DataBindings.Add(new Binding("SelectedItem", obs, "selected"));
  
     form.Controls.Add(nav);
     form.Controls.Add(lbox);
     lbox.Location = new System.Drawing.Point(30, 30);
  
     Application.Run(form);
    }
   }
  }
  </code></pre>
  
  <p>If you just select an item, move forward, select an item and then exit, it works as expected. But if you switch back and forth between the two outer objects with the navigator, the selected item seems to be overwritten with an incorrect value. It appears that every time the BindingNavigator moves to an element, the ListBox is told to move to the first item in its collection and thus overwrites whatever value used to be in the variable bound to its SelectedItem.</p>
  
  <p>Ideas on how to fix this? Thanks in advance.</p>
  
  <p>EDIT: Here is an archive of the example project, including a debug binary.</p>
  
  <p><a href="http://www.mediafire.com/?dzmqmz0mynj" rel="nofollow noreferrer">http://www.mediafire.com/?dzmqmz0mynj</a></p>
  
  <p>EDIT: Here is the helper function based on the accepted answer:</p>
  
  <pre><code>        public static void Bind(ListControl list, BindingSource outersource, string dataMember)
          {
              Binding bindSel = new Binding("SelectedItem", outersource, dataMember);
              list.DataBindings.Add(bindSel);
  
              outersource.CurrentChanged += delegate
              {
                  list.BeginInvoke(new MethodInvoker(bindSel.ReadValue));
              };
          }
  </code></pre>