title: Collapsed event fires on parent nodes of a TreeViewItem
body: <p>I have a WPF TreeView control defined like this:</p>
  
  <pre><code>&lt;TreeView x:Name="samplesTree" MouseDoubleClick="samplesTree_MouseDoubleClick"
            KeyUp="samplesTree_KeyUp"
            SelectedItemChanged="samplesTree_SelectedItemChanged"
            IsVisibleChanged="treeView_IsVisibleChanged"&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>I track what nodes have been expanded or collapsed using this code:</p>
  
  <pre><code>private List&lt;object&gt; SamplesExpandedTags = new List&lt;object&gt;();
  
  private void stag_Collapsed(object sender, RoutedEventArgs e)
  {
      object tag = (sender as TreeViewItem).Tag;
      if (SamplesExpandedTags.Contains(tag))
          SamplesExpandedTags.Remove(tag);
  }
  
  private void stag_Expanded(object sender, RoutedEventArgs e)
  {
      object tag = (sender as TreeViewItem).Tag;
      if (!SamplesExpandedTags.Contains(tag))
          SamplesExpandedTags.Add(tag);
  }
  
  ...
  
  // Items are created in code behind, not using binding to DataSource 
  TreeViewItem item = new TreeViewItem();
  item.Header = tv.NazevTypuVyrobku;
  item.Tag = string.Format("TV{0}",tv.TypVyrobkuID);
  item.Expanded += new RoutedEventHandler(stag_Expanded);
  item.Collapsed += new RoutedEventHandler(stag_Collapsed);
  </code></pre>
  
  <p>By debugging I have found, that when the child node is being Collapsed then also <code>Collapsed</code> event of parent node fires, so <code>stag_Collapsed</code> is run for the whole chain of parent elements of the actually collapsed node.   </p>
  
  <p>I have added this code at the beginning of the <code>stag_Collapsed</code> method:</p>
  
  <pre><code>if ((sender as TreeViewItem).IsExpanded) return;
  </code></pre>
  
  <p>Now the code works as it should, but why the Collapsed event fires on all the parent TreeViewItems and not only on the TreeViewItem actually being collapsed? I would expect that <code>IsExpanded</code> property is allways <code>false</code> on sender.</p>
  
  <hr>
  
  <p>The suggested solution to set:</p>
  
  <pre><code>e.Handled = true;
  </code></pre>
  
  <p>is not bullet proof, since this code:</p>
  
  <pre><code>subitem.Expanded += new RoutedEventHandler(stag_Expanded);
  subitem.Expanded += new RoutedEventHandler(sampleOperation_Expanded);
  subitem.Collapsed += new RoutedEventHandler(stag_Collapsed);
  </code></pre>
  
  <p>is working differently then this code:</p>
  
  <pre><code>subitem.Expanded += new RoutedEventHandler(sampleOperation_Expanded);
  subitem.Expanded += new RoutedEventHandler(stag_Expanded);
  subitem.Collapsed += new RoutedEventHandler(stag_Collapsed);
  </code></pre>
  
  <p>In the first case <code>sampleOperation_Expanded</code> does not get executed at all because of <code>e.Handled = true;</code> command.
  I thought <a href="https://stackoverflow.com/questions/1645478/order-of-event-handler-execution">that order of event handler execution</a> should not be significant?</p>