title: Which item was selected in my TreeView
body: <p>In my application I build a TreeView by a binding. The ItemSource contains a list of items of type X.<br>
  X has a property 'Properties' some of these properties are references to an item in the parent list.</p>
  
  <p>The TreeView starts at a selected root (an item from the list). All properties which are references are added to this root as TreeViewItems. However, they can be expanded too until the end is reached, or not; I mean that this could theoretically be an infinite recursion for it is possible that a reference refers to itself.</p>
  
  <p>The question now is, <strong>how do I know which Item is selected?</strong><br>
  I need to know the level and all its parents.</p>
  
  <p>I thought up these things:</p>
  
  <ul>
  <li>While expanding the tree, keep track of the levels and parents.<br>
  This does not work obviously because after  selecting an other item below a different parent, I lose track of the other path.</li>
  <li>Check for the <code>e.OldValue</code>. This also doesn't work for the same reason of the previous try.</li>
  <li>While expanding (and with that creating) the TreeView, construct a TreeView like variable in c# behind the code.<br>
  At first this seems to be quite close to the solution since you cannot remove nodes from the TreeView ones they are created. For example: You go to the 5th level and collapsed the first level. If you expand the first level you'll see the the path exacly the same as before the collapse.</li>
  </ul>
  
  <p>To create the TreeView in this way I had to override the DataTemplateSelector. (C#)</p>
  
  <pre><code>public class TreeViewDataTemplateSelector : DataTemplateSelector
  {
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          FrameworkElement element = container as FrameworkElement;
          if (element != null &amp;&amp; item != null)
          {
              if (item is Entity)
                  return element.FindResource("XTemplate") as DataTemplate;
              else
                  return element.FindResource("PropertyWithReferenceTemplate") as DataTemplate;
          }
          return null;
      }
  }
  </code></pre>
  
  <p>The Binding and HierarachialDataTemplate. (Xaml)</p>
  
  <pre><code>&lt;TreeView ItemsSource="{Binding ElementName=view, Path=XDefinition}" ItemTemplateSelector="{StaticResource treeViewDataTemplateSelector}"  SelectedItemChanged="TreeView_SelectedItemChanged"&gt;
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate x:Key="XTemplate" ItemsSource="{Binding Path=Properties}"&gt;
              &lt;TextBlock Text="{Binding Path=Name}" /&gt;                            
          &lt;/HierarchicalDataTemplate&gt;
          &lt;HierarchicalDataTemplate x:Key="PropertyWithReferenceTemplate" ItemsSource="{Binding Path=ReferenceToX.Properties,}"&gt;
              &lt;TextBlock Text="{Binding Path=Name}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Can anybody help me on this problem?</p>
  
  <p>If there is anything else you need to know please tell me and I'll update my question.</p>
  
  <p><strong>Solution</strong>
  I had to change my event SelectedItemChanged="TreeView_SelectedItemChanged" to TreeViewItem.Selected="TreeViewItem_Selected". With the help of the LogicalTreeHelper.GetParent i was able to retrieve the correct parent.</p>