title: Get TreeViewItem for TreeView logical element
body: <p>My tree view seems like this</p>
  
  <pre><code>&lt;TreeView x:Name="ArticlesTreeView" Grid.Column="0" AllowDrop="True"&gt;
              &lt;TreeView.Resources&gt;
                  &lt;HierarchicalDataTemplate   DataType="{x:Type structure:NewsPaperDocument}" ItemsSource="{Binding Children}"&gt;
                      &lt;TextBlock Text="{Binding Name}" Tag="{Binding Object}" FontWeight="Bold" /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperPage}" ItemsSource="{Binding Children}"&gt;
                      &lt;TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#00a300" /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperTitle}" ItemsSource="{Binding Children}"&gt;
                      &lt;TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#da532c" /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
                  &lt;DataTemplate DataType="{x:Type structure:NewsPaperBlock}"&gt;
                      &lt;TextBlock Text="{Binding Name}" Tag="{Binding Object}" Foreground="#2b5797" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TreeView.Resources&gt;
          &lt;/TreeView&gt;
  </code></pre>
  
  <p>In <code>ArticlesTreeView.SelectedItem</code> stores instance of classes <code>NewsPaperDocument, NewsPaperPage, etc</code>. How can I get <code>TreeViewItem</code> associated with <code>SelectedItem</code>? I try to use <code>VisualTreeHelper.GetParent(elem);</code>, but <code>SelectedItem</code> doesn't has type <code>DependencyObject</code></p>
  
  <p><strong>UPD1</strong> Add simple sample, that demonstrate problem. <code>item</code> in <code>ArticlesTreeView_SelectedItemChanged</code> always <code>null</code></p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Window x:Class="TestTree.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:testTree="clr-namespace:TestTree"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;TreeView x:Name="ArticlesTreeView" Grid.Column="0" AllowDrop="True" SelectedItemChanged="ArticlesTreeView_SelectedItemChanged"&gt;
          &lt;TreeView.Resources&gt;
              &lt;HierarchicalDataTemplate   DataType="{x:Type testTree:A}" ItemsSource="{Binding Children}"&gt;
                  &lt;TextBlock Text="{Binding Name}" FontWeight="Bold" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
              &lt;HierarchicalDataTemplate DataType="{x:Type testTree:B}" ItemsSource="{Binding Children}"&gt;
                  &lt;TextBlock Text="{Binding Name}" Foreground="#00a300" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type testTree:C}"&gt;
                  &lt;TextBlock Text="{Binding Name}" Foreground="#2b5797" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/TreeView.Resources&gt;
      &lt;/TreeView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>CS</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Windows;
  using System.Windows.Controls;
  
  namespace TestTree
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              var a = new A
              {
                  Name = "a",
                  Children = new List&lt;B&gt;
                  {
                      new B
                      {
                          Name = "b1",
                          Children = new List&lt;C&gt;
                          {
                              new C{Name = "c1"},
                              new C{Name = "c2"},
                              new C{Name = "c3"}
                          },
                      },
                      new B
                      {
                          Name = "b2",
                          Children = new List&lt;C&gt;
                          {
                              new C{Name = "c1"},
                              new C{Name = "c2"},
                              new C{Name = "c3"}
                          },
                      },
                      new B
                      {
                          Name = "b3",
                          Children = new List&lt;C&gt;
                          {
                              new C{Name = "c1"},
                              new C{Name = "c2"},
                              new C{Name = "c3"}
                          },
                      }
                  }
              };
              ArticlesTreeView.ItemsSource = new List&lt;A&gt; { a };
          }
  
          private void ArticlesTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e)
          {
              ItemContainerGenerator gen = ArticlesTreeView.ItemContainerGenerator;
              var item = gen.ContainerFromItem(ArticlesTreeView.SelectedItem);
          }
      }
  
      internal class A
      {
          public string Name { set; get; }
  
          public List&lt;B&gt; Children { set; get; }
      }
  
      internal class B
      {
          public string Name { set; get; }
  
          public List&lt;C&gt; Children { set; get; }
      }
  
      internal class C
      {
          public string Name { set; get; }
      }
  }
  </code></pre>