title: How to bind Custom Object to ListBox in WPF
body: <p>I am new to WPF so please accept my apologies if my question is stupid.</p>
  
  <p>I am creating a food ordering system which consists of 2 main sections, the "Food Menu" and the "Order List". When user chooses an item from the food menu, it will be added to the a listbox control which represents the order list.</p>
  
  <p>I have created a few custom objects: <code>Order</code>, <code>OrderLine</code>, <code>Item</code>, as well as a Collection class, <code>OrderLineCollection</code>, for "OrderLine". They look like the following:
  </p>
  
  <pre><code>public class Order
  {
      public string ID { get; set; }
      public DateTime DateTime { get; set; }
      public double TotalAmt { get; set; }
      public OrderLineCollection LineItems { get; set; }
  }
  
  public class OrderLine
  {
      public Item Item { get; set; }
      public double UnitPrice { get; set; }
      public int Quantity { get; set; }
      public double SubTotal { get { return unitPrice * quantity; } }
  }
  
  [Serializable]
  public class OrderLineCollection : CollectionBase
  {
      public OrderLine this[int index]
      {
          get { return (OrderLine)List[index]; }
          set { List[index] = value; }
      }
  }
  
  public class Item
  {
      public string ID { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public double UnitPrice { get; set; }
      public byte[] Image { get; set; }
  }
  </code></pre>
  
  <p>My <code>ListBox</code> control has a <code>DataTemplate</code> so that more details are shown for each item. The XAML as below:
  </p>
  
  <pre><code>&lt;Page x:Class="FoodOrdering.OrderList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Order List"&gt;
      &lt;ListBox Name="lbxOrder" HorizontalContentAlignment="Stretch"&gt;            
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid Margin="5"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="*"/&gt;
                          &lt;ColumnDefinition Width="30"/&gt;
                          &lt;ColumnDefinition Width="80"/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition/&gt;
                          &lt;RowDefinition Height="40"/&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;TextBlock Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Text="{Binding item.name}"/&gt;
                      &lt;TextBlock Grid.Row="0" Grid.Column="1" Text="x "/&gt;
                      &lt;TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding quantity}" Margin="10,0,0,0"/&gt;
                      &lt;TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding subTotal, Converter={StaticResource priceConverter}}" HorizontalAlignment="Right"/&gt;
                      &lt;Button Grid.Row="1" Grid.Column="2" Margin="0,5,0,0" Click="btnDelete_Click"&gt;Delete&lt;/Button&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>so when items are added, the ListBox will look like the below image:
  <a href="https://dl.dropbox.com/u/6322828/orderList.png" rel="nofollow">https://dl.dropbox.com/u/6322828/orderList.png</a></p>
  
  <p>In the code-behind, I have created a static variable <code>currentOrder</code> for storing the current order and it will be used by other classes/methods.</p>
  
  <p>And every time its value is changed, the following stupid method <code>LoadCurrentOrder()</code> is called to refresh the ListBox view.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class OrderList : Page
  {
      public static Order currentOrder = new Order();
      public OrderList()
      {
          InitializeComponent();
          LoadCurrentOrder();
      }
  
      public void LoadCurrentOrder()
      {
          lbxOrder.Items.Clear();
          foreach (OrderLine ol in currentOrder.LineItems)
              lbxOrder.Items.Add(ol);
      }
  }
  </code></pre>
  
  <p>My problem is how can I bind the data in an elegant way (such as using <code>Resources</code> <code>ItemsSource</code> and so on) instead of using the above method, so that the ListBox will update automatically every time the value of the variable is changed?</p>
  
  <p>I tried 
  </p>
  
  <pre><code>lbxOrder.ItemsSource = currentOrder; 
  </code></pre>
  
  <p>but it does not work as currentOrder is not a <code>System.Collections.IEnumerable</code> object.</p>