title: How can I bind a ListBox control to a List in WPF?
body: <p>I want to create a 2 way bind between a listbox and a .NET list.</p>
  
  <p>In my GUI, I have a listbox, a textbox and add and remove buttons.
  The listbox displays cars, and my goal is to create a two-way bind between the .Net car list and the listbox: when the user enters a car into the textbox, it gets updated only in the .Net list, and the listbox is updated automatically.</p>
  
  <p>When the user press the GUI "remove" button, a car gets removed from the GUI and the .Net list is updated automatically.</p>
  
  <p>I've started to write the xaml code, but figured that I don't actually know how to do the binding on both sides (c# and xaml):</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:c="clr-namespace:WpfApplication1"
      Title="Window1" Height="300" Width="369" Loaded="Window_Loaded"&gt;
      &lt;Window.Resources&gt;
          &lt;ObjectDataProvider x:Key="carsData" 
                          ObjectType="{x:Type c:Window1}" /&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid Width="332"&gt;
          &lt;ListBox Margin="10,62,0,100" Name="myListBox" HorizontalAlignment="Left" Width="120" ItemsSource="{Binding Source={StaticResource CarsData}}"/&gt;
          &lt;Button Height="23" Margin="66,0,0,65" Name="addBtn" VerticalAlignment="Bottom" Click="addBtn_Click" HorizontalAlignment="Left" Width="64"&gt;add&lt;/Button&gt;
          &lt;TextBox Margin="10,0,0,64.48" Name="myTextBox" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="47" /&gt;
          &lt;Button Height="23" Margin="66,0,0,33" Name="removeButton" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="64" Click="removeButton_Click"&gt;Remove&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>There is my c# code:</p>
  
  <pre><code>public partial class Window1 : Window
  {
      MyModel listMgr;
      ObservableCollection&lt;Car&gt; carList;
  
      public Window1()
      {
          InitializeComponent();
          listMgr = new MyModel();
      }
  
      private void addBtn_Click(object sender, RoutedEventArgs e)
      {
          listMgr.add(new Car(0, myTextBox.Text, 2011));
  
      }
  
      private void removeButton_Click(object sender, RoutedEventArgs e)
      {
          //myListBox.Items.RemoveAt(0);
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          carList = listMgr.getList();
          myListBox.DataContext = carList;
          //secondListBox.DataContext = carList;
      }
  }
  </code></pre>