title: ListBox databinding not updating properly
body: <p>I am getting a strange result where the data binding to a <code>ListBox</code> works when the <code>ObservableCollection</code> it is bound to is populated in the view model constructor but not when I move the same <code>ObservableCollection</code> population code to an <code>Button</code> event (tried from code behind event handler on the view and from a <code>RoutedCommand</code>). There are no <code>async</code> web services or background threads.</p>
  
  <p>When I step through the code, the <code>ObservableCollection</code> in the view model is still populated when called from the button event but the data is not updated on the <code>ListBox</code>. The XAML is identical in both.</p>
  
  <p>Here is some code:</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>                &lt;ListBox ItemsSource="{Binding Books}" BorderBrush="{x:Null}" Name="Results"&gt;
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Vertical"&gt;
                              &lt;TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" /&gt;
                              &lt;TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
              &lt;/ListBox&gt;
         ...
              &lt;Button Command="{x:Static commands:BookQueryCommands.Query}" Content="Search" Width="119" Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,25,0,0"/&gt;
  </code></pre>
  
  <p><strong>View code behind</strong></p>
  
  <pre><code>public partial class BookExplorer : UserControl
  {
      private BookExplorerViewModel vm;
  
      public BookExplorer()
      {
          this.InitializeComponent();
  
          vm = new BookExplorerViewModel();
          this.DataContext = vm;
  
          this.CommandBindings.Add(new CommandBinding(BookQueryCommands.Query, ExecuteQuery));
      }
  
      public void ExecuteQuery(object sender, ExecutedRoutedEventArgs e) {
          vm.ExecuteBookQuery();
      }
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>    public class BookExplorerViewModel : DependencyObject
  {   
      private IBookListProvider bookProvider;
      private bool canQuery = true;
  
      public ObservableCollection&lt;BookModel&gt; Books { get; set; }
      public string Title { get; set; }
      public string ID { get; set; }
      public DateTime LastModifiedDate { get; set; }
  
      public BookExplorerViewModel() {
          bookProvider = new BookListProvider();
      }
  
      public void ExecuteBookQuery() {
          Books = bookProvider.DocumentsQuery("temp");
      }
  }
  
  // bookProvider.DocumentsQuery("temp") just fills with dummy data.
  // Binding works fine if the code in ExecuteBookQuery() is in the VM constructor
  // Books is still populated fine in either case just binding is not updated
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class BookModel : INotifyPropertyChanged
  {
      private string title;
      public string Title {
          get { return title; }
          set {
              if (value != this.title) {
                  this.title = value;
                  NotifyPropertyChanged(Title);
              }
          }
      }
  
      private string id;
      public string ID {
          get { return id; }
          set {
              if (value != this.id) {
                  this.id = value;
                  NotifyPropertyChanged(ID);
              }
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void NotifyPropertyChanged(String info) {
          if (PropertyChanged != null) {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  }
  </code></pre>