title: WPF MVVM application - ICollectionView/Threading/General Questions
body: <p>I have a few questions regarding to building WPF MVVM applications.</p>
  
  <p>1) I'm using ICollectionView objects for databound controls such as ListView and ComboBox. I found this was the simplest way of gaining access to/tracking the selected item of these controls. What is the best way to replace the contents of ICollectionView? Currently I'm doing it like so:</p>
  
  <pre><code>    private ICollectionView _files;
  
  public ICollectionView Files {
      get { return _files; }
  }
  
  void _service_GetFilesCompleted(IList&lt;SomeFile&gt; files) {
      this.IsProcessing = false;
      _files = CollectionViewSource.GetDefaultView(files);
      _files.CurrentChanged += new EventHandler(FileSelectionChanged);
      OnPropertyChanged("Files");
  }
  </code></pre>
  
  <p>I didn't know whether it was necessary to reattach the handler every time I refresh the list of files?</p>
  
  <p>2) Now that I've got my head round it, I am starting to like the MVVM pattern. However, one concept I'm not completely sure about is how to send notifications back down to the view. Currently I am doing this by binding to properties on my ViewModel. For example, in the above code I have an "IsProcessing" property that I use to determine whether to display a ProgressBar. Is this the recommended approach?</p>
  
  <p>3) Following on from 2) - there doesn't seem to be a standard way to handle exceptions in an MVVM application. One thought I had was to have one method on my ViewModel base class that handled exceptions. I could then inject an IMessagingService that was responsible for relaying any error messages. A concrete implementation of this could use MessageBox.</p>
  
  <p>4) I have a few tasks that I want to perform asynchronously. Rather than adding this logic directly in my service I created a decorator service that runs the underlying service methods on a new thread. It exposes a number of events that my ViewModel can then subscribe to. I have listed the code below. I understand that BackgroundWorker is a safer option but did not know whether it was suitable for running multiple asynchronous tasks at once?:</p>
  
  <pre><code>    public void BeginGetFiles()
      {
          ThreadStart thread = () =&gt; {
              var result = _serviceClient.GetUserFiles(username, password);
              GetFilesCompleted(result.Files);
          };
  
          new Thread(thread).Start();
      }
  </code></pre>
  
  <p>Finally, I realize that there are a number of MVVM frameworks projects that handle some of these requirements. However, I want to understand how to achieve the above using built-in functionality.</p>
  
  <p>Thanks</p>