title: An ItemsControl is inconsistent with its items source - WPF Listbox
body: <p>I have a WPF window containing a ListBox control that is populated when a button click method is executed.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0"&gt;                      
                      &lt;ListBox.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Source="C:\Users\Test\Desktop\Project\ACME-WPF\ACME-WPF\window-new-3.ico" Margin="5" Width="50"/&gt;
                                  &lt;Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"&gt;&lt;/Button&gt;
                                  &lt;Button Name="ThirdPartyPostoneButton" Content="Postpone" Click ="PostponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"&gt;&lt;/Button&gt;
                                  &lt;TextBlock FontWeight="Bold" Text="{Binding Item2.Name}" Margin="12,25,0,0"/&gt;
                                  &lt;TextBlock FontWeight="Bold" Text="{Binding Item2.RequiredVersion}" Margin="3,25,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Item2.CustomUIMessage}" Margin="10,25,0,0" TextWrapping="Wrap" Foreground="Red"/&gt;
                                  &lt;TextBlock Text="You have used " Margin="3,25,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Item3.UsedDeferrals}" Margin="3,25,0,0"/&gt;
                                  &lt;TextBlock Text=" of " Margin="3,25,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Item2.MaxDefferals}" Margin="3,25,0,0"/&gt;
                                  &lt;TextBlock Text=" deferrals for this update." Margin="3,25,0,0"/&gt;
                              &lt;/StackPanel&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListBox.ItemTemplate&gt;
                  &lt;/ListBox&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code> private void CheckforThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
      {
          CheckforThirdPartyUpdatesButton.IsEnabled = false;
  
          worker = new BackgroundWorker();
          worker.WorkerReportsProgress = true;
          worker.WorkerSupportsCancellation = true;
  
          worker.DoWork += delegate(object s, DoWorkEventArgs args)
          {
              MainEntry.checkFor3PUpdates();
          };
  
          worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
          {
  
          };
  
          worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
          {
  
              ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;
              CheckforThirdPartyUpdatesButton.IsEnabled = true;
          };
  
          worker.RunWorkerAsync();
      }
  </code></pre>
  
  <p>Everything up to this point functions as expected and the listbox is populated with multiple rows of items depending on how many items are in list <code>ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;</code>. However, if I interact with the listbox items at all, an InvalidOperationException is thrown with inner exception "An ItemsControl is inconsistent with its items source."</p>
  
  <p>Can someone help me understand what's happening?</p>