title: How to filter a wpf treeview hierarchy using an ICollectionView?
body: <p>I have a hypothetical tree view that contains this data:</p>
  
  <pre><code>RootNode
     Leaf
     vein
  SecondRoot
     seeds
     flowers
  </code></pre>
  
  <p>I am trying to filter the nodes in order to show only the nodes that contain a certain text. Say if I specify "L", the tree will be filtered and show only RootNode->Leaf and SecondRoot->flowers (because they both contain the letter L).</p>
  
  <p>Following the m-v-vm pattern, I have a basic TreeViewViewModel class like this:</p>
  
  <pre><code>public class ToolboxViewModel
  {
      ...
      readonly ObservableCollection&lt;TreeViewItemViewModel&gt; _treeViewItems = new ObservableCollection&lt;TreeViewItemViewModel&gt;();
      public ObservableCollection&lt;TreeViewItemViewModel&gt; Headers
      {
          get { return _treeViewItems; }
      }
  
      private string _filterText;
      public string FilterText
      {
          get { return _filterText; }
          set
          {
              if (value == _filterText)
                  return;
  
              _filterText = value;
  
              ICollectionView view = CollectionViewSource.GetDefaultView(Headers);
              view.Filter = obj =&gt; ((TreeViewItemViewModel)obj).ShowNode(_filterText);
          }
      }
      ...
  }
  </code></pre>
  
  <p>And a basic TreeViewItemViewModel:</p>
  
  <pre><code>public class ToolboxItemViewModel
  {
      ...
      public string Name { get; private set; }
      public ObservableCollection&lt;TreeViewItemViewModel&gt; Children { get; private set; }
      public bool ShowNode(string filterText)
      {
          ... return true if filterText is contained in Name or has children that contain filterText ... 
      } 
      ...
  }
  </code></pre>
  
  <p>Everything is setup in the xaml so I see the treeview and search box.</p>
  
  <p>When this code is exercised, the filter only applies to the Root nodes which is insufficient. Is there a way to make the filter trickle down in the hierarchy of nodes so that my predicate is called for every node ? In other words, can the filter be applied to the TreeView as a whole ?</p>