title: WPF Hide items with trigger in treeview
body: <p>I've got a <code>TreeView</code>, in which I use a <code>HierarchicalDataTemplate</code>. With this, I color my different items (all of the same type). </p>
  
  <p>With a click on a <code>CheckBox</code> in the Page, I want to hide some of the items (with a certain Property). I've tested many code, but nothing works properly. I'm looking for answers ...</p>
  
  <p>Here's a sample of my code :</p>
  
  <pre><code>public partial class Window1 : Window
  {
      public Window1()
      {
          InitializeComponent();
  
          TreeElements tRoot = new TreeElements("Root", false);
          TreeElements t1 = new TreeElements("Node 1", false);
          TreeElements t11 = new TreeElements("Node 1-1", true);
          t1.Children.Add(t11);
          TreeElements t12 = new TreeElements("Node 1-2", false);
          t1.Children.Add(t12);
          tRoot.Children.Add(t1);
          TreeElements t2 = new TreeElements("Node 2", false);
          TreeElements t21= new TreeElements("Node 2-1", false);
          TreeElements t211 = new TreeElements("Node 2-1-1", false);
          t21.Children.Add(t211);
          t2.Children.Add(t21);
          tRoot.Children.Add(t2);
          trv.Items.Add(tRoot);
      }
  }
  
  public class TreeElements
  {
      public string Description { get; set; }
      public List&lt;TreeElements&gt; Children { get; set; }
  
      public TreeElements(string description, bool error)
      {
          Description = description;
          _error = error;
          Children = new List&lt;TreeElements&gt;();
      }
  
      private bool _error;
  
      public bool Error
      {
          get
          {
              bool bValue = _error;
              foreach (TreeElements child in Children)
                  bValue = bValue || child.Error;
              return bValue;
          }
      }
  }
  </code></pre>
  
  <p>And the XAML :</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication2.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
  
      &lt;Window.Resources&gt;
          &lt;HierarchicalDataTemplate x:Key="HDT_items" ItemsSource="{Binding Path=Children}"&gt;
              &lt;TextBlock Text="{Binding Path=Description}" x:Name="txt" /&gt;
              &lt;HierarchicalDataTemplate.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding Path=Error}" Value="true"&gt;
                      &lt;Setter TargetName="txt" Property="Foreground" Value="Red" /&gt;
                  &lt;/DataTrigger&gt;
              &lt;/HierarchicalDataTemplate.Triggers&gt;
          &lt;/HierarchicalDataTemplate&gt;    
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;CheckBox x:Name="chk"&gt;Mask some items&lt;/CheckBox&gt;
  
          &lt;TreeView Grid.Row="1" x:Name="trv" ItemTemplate="{StaticResource HDT_items}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>I want, when the checkbox is checked, to not display the nodes who are with Error=false, but without changing the data source. Is it possible, and how ?</p>