title: Filter an observable collection
body: <p>I have a <code>ListView</code> control that displays items from an observable collection. These items need to be filtered. I am able to do that with a <code>CollectionViewSource</code>, but the filter needs to be updated each time an item changes.</p>
  
  <p>My items looks like this:</p>
  
  <pre><code>enum Status {Done, Failed, Skipped, ...}
  
  class Project {
    public string Name {get;set;}
    public Status Status {get;set;}
    // etc. etc.
  }
  
  class ProjectViewModel : INotifyPropertyChanged {
    private Project project;
  
    public ProjectBuildInfoViewModel(ProjectBuildInfo project)
    {
      this.project = project;
    }
  
    public string Name
    {
       get { return project.Name; }
       set { project.Name = value; OnPropertyChanged("Name"); }
    }
  
    // etc. etc.
  }
  
  class CollectionViewModel {
    private ObservableCollection&lt;ProjectViewModel&gt; projects = 
               new ObservableCollection&lt;ProjectViewModel&gt;();
  
    public ObservableCollection&lt;ProjectViewModel&gt; Collection
    {
       get { return projects; }
       private set {projects = value; }
    } 
  }
  </code></pre>
  
  <p>Then I have this <code>ListView</code> whose <code>ItemSource</code> is bound to the collection.</p>
  
  <pre><code>// member of the user control class
  private CollectionViewModel collection = new CollectionViewModel();
  
  // in the constructor
  listView.ItemSource = collection.Collection.
  </code></pre>
  
  <p>This doesn't filter anything. So I have these check boxes and they should indicate which items (depending of the state) should be displayed. I have used then a <code>CollectionViewSource</code>:</p>
  
  <pre><code>private void UpdateView()
  {
    var source = CollectionViewSource.GetDefaultView(collection.Collection);
    source.Filter = p =&gt; Filter((ProjectViewModel)p);
    listStatus.ItemsSource = source;
  }
  </code></pre>
  
  <p>The filter method looks like this:</p>
  
  <pre><code>private bool Filter(ProjectViewModel project)
  {
       return (ckFilterDone.IsChecked.HasValue &amp;&amp; ckFilterDone.IsChecked.Value &amp;&amp; project.Status == Status.Done) ||
              (ckFilterFailed.IsChecked.HasValue &amp;&amp; ckFilterFailed.IsChecked.Value &amp;&amp; project.Status == Status.Failed) ||
              (ckFilterSkipped.IsChecked.HasValue &amp;&amp; ckFilterSkipped.IsChecked.Value &amp;&amp; project.Status == Status.Skipped);
  }
  </code></pre>
  
  <p>This has the disadvantage that it captures the values of the checkboxes, so I have to call this method (<code>UpdateView</code>) each time a checkbox is checked. But it works.</p>
  
  <p>However, the item state does change and if "done" is not checked for instance, when an item goes into "done" it should be removed from the view. Obviously that doesn't change unless I again call <code>UpdateView</code>. So I need to call this method each time something changes. That looks ugly and inefficient to me.</p>
  
  <p>So my question is, can this be done in a nicer way?</p>