Checkbox binding to change the ListView's items source I am using a checkbox to filter some components on my ListView by changing the variable's content which is bound to this ListView:

C#



#region Constructor

void CreateAllParts()
{
    // GetParts() returns List&lt;Part&gt;. Now we can pour it's results 
    // using the following select in List&lt;EditPartViewModel&gt; on the 
    // fly.
    // allpvms: All Part View Models.
    allpvms = _partRepository.GetParts().Select(p =&gt; new EditPartViewModel(p)).ToList();

    foreach (EditPartViewModel cvm in allpvms)
    {
        cvm.PropertyChanged += this.OnPartViewModelPropertyChanged;
    }

    this.AllParts = new ObservableCollection&lt;EditPartViewModel&gt;(allpvms);
    this.AllParts.CollectionChanged += this.OnCollectionChanged;
}


void LoadErrorParts()
{
    if (_errorParts)
    {
        this.AllParts = new ObservableCollection&lt;EditPartViewModel&gt;(allpvms.Where(p =&gt; p.Price == null));
        this.AllParts.CollectionChanged += this.OnCollectionChanged;
    }
    else
    {
        this.AllParts = new ObservableCollection&lt;EditPartViewModel&gt;(allpvms);
        this.AllParts.CollectionChanged += this.OnCollectionChanged;
    }
}

#endregion // Constructor

#region Public Interface

/// &lt;summary&gt;
/// Returns a collection of all the EditPartViewModel objects.
/// &lt;/summary&gt;
public ObservableCollection&lt;EditPartViewModel&gt; AllParts
{
    get
    {
        return _allParts;
    }
    set
    {
        _allParts = value;
        OnPropertyChanged("AllParts");
    }
}

public bool ErrorParts
{
    get
    {
        return _errorParts;
    }
    set
    {
        _errorParts = value;
        OnPropertyChanged("ErrorParts");
        LoadErrorParts();
    }
}

#endregion // Public Interface


XAML



&lt;CheckBox DockPanel.Dock="Top" 
          IsChecked="{Binding ErrorParts, Mode=TwoWay}"
          HorizontalAlignment="Right"
          Content="Parts with Error"
          /&gt;

&lt;ListView
  AlternationCount="2"
  d:DataContext="{d:DesignInstance Type=vm:MockViewModel, IsDesignTimeCreatable=True}"
  ItemsSource="{Binding AllParts}"
  &gt;
  &lt;ListView.ItemContainerStyle&gt;
    &lt;Style TargetType="{x:Type ListViewItem}"&gt;
      &lt;EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick" /&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
    &lt;/Style&gt;
  &lt;/ListView.ItemContainerStyle&gt;
&lt;/ListView&gt;


ListViews content get collected by a DataTemplate in the Resources.

Is the way I use the binding concept look okay? Any corrections or "good practice" methods I should consider when doing similar procedures?
You should probably bind ItemsSource to CollectionView, instead of binding to ObservableCollection. This way you don't have to recreate collection every time you want to apply your filter. May look like this:

//constructor
Parts = new ObservableCollection&lt;EditPartViewModel&gt;(allpvms);
PartsCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(Parts);

...............

public ListCollectionView PartsCollectionView { get; private set; }

public bool ErrorParts
{
    get
    {
        return _errorParts;
    }
    set
    {
        _errorParts = value;
        OnPropertyChanged("ErrorParts");
        PartsCollectionView.Filter = value ? (Predicate&lt;Object&gt;)(p =&gt; ((EditPartViewModel)p).Price == null) : null;
    }
}


Also if your viewmodles can contain errors, you should probably move your error-checking logic inside those viewmodels. So instead of writing p.Price == null you would write p.HasErrors or something.

You should also indicate, that ErrorParts is actually a bool property, unlike AllParts which is a collection. DisplayErrors or ShowErrors or FilterErrors etc. are better names for this property in my opinion.