title: How to reflect order changes in a WPF listbox
body: <p>I have (what I feel to be) a very simple scenario that I can't seem to figure out. I've looked around here (ie. StackOverflow.com) for a solution, but can't seem to find anybody with my exact problem. I'm not sorting my listbox on a criteria; I simply want my listbox to reflect the order of the items in my collection.</p>
  
  <p>I'm using Caliburn.Micro to bind an underlying collection to a WPF ListBox. I simply want to be able to allow the user to move items up and down the list using buttons that I've provided. The functionality works great! However, the ListBox will not reflect the new collection's order. </p>
  
  <p>Here is a simple ViewModel:</p>
  
  <pre><code>public class MyViewModel : PropertyChangedBase
  {
    private readonly IObservableCollection&lt;SimpleItem&gt; _items;
    private SimpleItem _selectedItem;
  
    public MyViewModel()
    {
      _items = new BindableCollection&lt;SimpleItems&gt;();
      _items.Add(new SimpleItem("Item 1"));
      _items.Add(new SimpleItem("Item 2"));
      _items.Add(new SimpleItem("Item 3"));
    }
    public IObservableCollection&lt;SimpleItem&gt; Items
    {
      get { return _items; }
    }
  
    public SimpleItem SelectedItem
    {
      get { return _selectedItem; }
      set
      {
        _selectedItem = value;
        NotifyOfPropertyChanged(() =&gt; SelectedItem);
      }
    }
  }
  </code></pre>
  
  <p>Here is the Xaml that binds to my ViewModel (again, using Caliburn.Micro):</p>
  
  <pre><code>&lt;ListBox x:Name="Items"&gt;
    &lt;ListBox.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;TextBlock Text="{Binding Caption}"/&gt;
      &lt;/DataTemplate&gt;
    &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>The results display the items in my ListBox perfectly:</p>
  
  <pre><code>Item 1
  Item 2
  Item 3
  </code></pre>
  
  <p>Now, assume the user wants to move <code>Item 2</code> above <code>Item 1</code>. They select <code>Item 2</code>, and then click a button named <code>MoveUp</code>. I have a method in my ViewModel called MoveUp() that does the following logic (simplified for brevity):</p>
  
  <pre><code>public void MoveUp()
  {
    var item = SelectedItem;
    var index = Items.IndexOf(item);
  
    Items.Remove(item);
    Items.Insert(index - 1, item);
  
    SelectedItem = item;
  }
  </code></pre>
  
  <p>Unfortunately, the ListBox does not reflect the new order... What is the best way to tell the view that the listbox to refresh the display <em>without</em> resorting to the code-behind? </p>
  
  <h2>UPDATE: July 20, 2011</h2>
  
  <p>I found the problem. The problem was that my collection of SimpleItems was actually a subset of a parent 'SimpleItem'. Therefore, when the user clicked on 'MoveUp', I would do a lookup on the parent SimpleItem and reference it's children directly. The problem was that the children were not bound to the IObservableCollection instance. I had to change my logic to find the observable collection instead and it works now! Thank you @devdigital for your help!</p>
  
  <p>-= Luc</p>