title: CollectionViewSource, how to filter data?
body: <p>I am binding a ComboBox to Entities but I want the data filtered.</p>
  
  <p>Up to now I have tried two ways:</p>
  
  <ul>
  <li>"simple" one:  Apply the filter directly to the ObjectSet throught
  LINQ to Entities   </li>
  <li>setting a filtering event handler as described on
  <a href="http://msdn.microsoft.com/en-us/library/ms752348.aspx" rel="noreferrer">msdn</a></li>
  </ul>
  
  <p>I am satisfied by the first approach, above all because the query generated to the database contains a WHERE clause, so not all the whole data have to be retrieved from the remote db....</p>
  
  <p>However, the #2 approach is by far more flexible, if at runtime i'd like to change the filtering applied... I have followed the example on msdn, but I get an exception, why?</p>
  
  <p>So, my questions are:<br>
  1. Which approach is better<br>
  2. Why I get the exception?</p>
  
  <p>Here is my code:</p>
  
  <pre><code> private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          //Do not load your data at design time.
          if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
          {
              //Load your data here and assign the result to the CollectionViewSource.
              System.Windows.Data.CollectionViewSource myCollectionViewSource =
                  (System.Windows.Data.CollectionViewSource)
                  this.Resources["tSCHEDEViewSource"];
  
              // If I use this I get the data filtered on startup, but is it the right mode?
              //myCollectionViewSource.Source = _context.TSCHEDE.Where(s =&gt; s.KLINEA == kLinea &amp;&amp; s.FCANC == "T").OrderBy(s =&gt; s.DSCHEDA).OrderByDescending(s =&gt; s.DSTORICO);
  
              // Instead If I apply my custom filtering logic
              myCollectionViewSource.Filter += new FilterEventHandler(filterSource);
  
              myCollectionViewSource.Source = _context.TSCHEDE; // ... Here i get an exception: 
              // 'System.Windows.Data.BindingListCollectionView' view does not support filtering. ???
          }
      }
  
  
      private void filterSource(object sender, FilterEventArgs e)
      {
          TSCHEDE scheda = e.Item as TSCHEDE;
          if (scheda != null)
          {
              if (scheda.KLINEA == 990)
              {
                  e.Accepted = true;
              }
              else
              {
                  e.Accepted = false;
              }
          }
      }
  </code></pre>
  
  <p><strong>EDIT</strong>: I have tried implementing the Filter property on the View rather than setting the EventHandler:</p>
  
  <pre><code>myCollectionView = (BindingListCollectionView)myCollectionViewSource.View;
  myCollectionView.Filter = new Predicate&lt;object&gt;(Contains);
  
  public bool Contains(object de)
      {
          TSCHEDE scheda = de as TSCHEDE;
          return (scheda.KLINEA == 990);
      }
  </code></pre>
  
  <p>And now I get the <em>not so useful</em> exception:</p>
  
  <blockquote>
    <p>System.NotSupportedException: Specified method is not supported.
       at System.Windows.Data.CollectionView.set_Filter(Predicate`1 value)</p>
  </blockquote>
  
  <p><strong>EDIT</strong></p>
  
  <p>XAML code:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE, CreateList=True}"  &gt;
      &lt;/CollectionViewSource&gt;
      &lt;DataTemplate x:Key="SchedaTemplate"&gt;
          &lt;StackPanel Orientation="Horizontal" &gt;
              &lt;TextBlock Text="{Binding Path=KSCHEDA}" Width="60"&gt;&lt;/TextBlock&gt;
              &lt;TextBlock Text="{Binding Path=DArticolo}" Width="200"&gt;&lt;/TextBlock&gt;
              &lt;TextBlock Text=" - " Width="40"&gt;&lt;/TextBlock&gt;
              &lt;TextBlock Text="{Binding Path=DSTORICO}" Width="150"&gt;&lt;/TextBlock&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid Background="PapayaWhip" DataContext="{StaticResource tSCHEDEViewSource}" DataContextChanged="StartHere" Name="rootGrid"&gt;
      &lt;ComboBox ItemTemplate="{StaticResource SchedaTemplate}" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="23,129,0,0" Name="tSCHEDEComboBox1" SelectedValuePath="KSCHEDA" VerticalAlignment="Top" Width="393"&gt;
          &lt;ComboBox.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;VirtualizingStackPanel /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ComboBox.ItemsPanel&gt;
      &lt;/ComboBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Now I am thinking the problem is in XAML Binding, not in code behind...</strong></p>