title: WPF MVVM update collections so that UI updates
body: <p>I want to update my UI. Should I use BackgroundWorker? Do I put the BackgroundWorker in the MainWindowViewModel and instantiate the repositories again, or do I put it in the OrdersQueueViewModel and do something with the properties? </p>
  
  <p>The UI just displays the contents of lists created by LINQ. The lists are <code>ObservableCollection</code> and are properties of the OrdersQueueViewModel. I have a ViewModel MainWindowViewModel that creates a collection ViewModels, so that I can bind to that collection from the MainWindow.xaml (view).</p>
  
  <p>MainWindowViewModel.cs:</p>
  
  <pre><code>public MainWindowViewModel()
  {
      _printQueueRepos = new OrdersPrintQueueRepository();
      _holdQueueRepos = new OrdersHoldQueueRepository();            
      _linesToPickRepos = new LinesToPickRepository();
      _linesPerHourRepos = new LinesPerHourRepository();
  
      //create an instance of viewmodel and add it to the collection            
      OrdersQueueViewModel viewModel = new OrdersQueueViewModel(_printQueueRepos, _holdQueueRepos, _linesToPickRepos, _linesPerHourRepos);
      this.ViewModels.Add(viewModel);              
  }
  </code></pre>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;DataTemplate DataType="{x:Type vm:OrdersQueueViewModel}"&gt;
          &lt;vw:OrdersQueueView&gt;&lt;/vw:OrdersQueueView&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Example of a property in the OrderQueueViewModel that uses a repository:</p>
  
  <pre><code>public ObservableCollection&lt;LinesToPick&gt; LinesToPick
  {
      get
      {
          return new ObservableCollection&lt;LinesToPick&gt;(_linesToPickRepos.GetLinesToPick());
      }
  }
  </code></pre>
  
  <p>So I have<code>LinesToPick</code> bound in the OrdersQueueView, and as the database updates the lists should change in the UI. I'v spent some time reading about BackgroundWorker, but I'm not quite sure what to do to update the lists. I'm hoping because they are <code>ObservableCollections</code> I can just "refresh" them and they will use INotifyPropertyChanged and update the UI automatically. Very new to all this, trying to get my head around it, thanks in advance for any help.</p>
  
  <p><strong>EDIT:</strong> Using James's suggestion I have ended up with this In my OrdersQueueViewModel. However I am getting the error "<strong>This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread</strong>", when the code gets to .Clear() on the 2 lists, which is what I thought the dispatcher was used for. Any suggestions?</p>
  
  <pre><code>Action workAction = delegate
  {
      _worker = new BackgroundWorker();
      _worker.DoWork += delegate
      {
          LinesThroughput.Clear();
          LinesToPick.Clear();
  
          //refresh LinesToPick
          foreach (var item in _linesToPickRepos.GetLinesToPick())
          {
              LinesToPick.Add(item);
          }
  
         //refresh LinesThroughput
         List&lt;LinesThroughput&gt; Lines = new List&lt;LinesThroughput&gt;    (_linesPerHourRepos.GetLinesThroughput());
  
         foreach (var item in GetLinesThroughput(Lines))
         {
             LinesThroughput.Add(item);
         }
     };
     _worker.RunWorkerAsync();
  };
  Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, workAction);
  </code></pre>