title: How to access a treeviewitem with in a treeviewitem in wpf?
body: <p>I have populated my treeview using hierarchichal data template and using data template for sub menu items.</p>
  
  <p>Here is what my xaml tree structure taken from wpf snoop looks like
  <a href="https://i.stack.imgur.com/H8Vab.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H8Vab.png" alt="enter image description here"></a></p>
  
  <pre><code>TreeViewItem item = (TreeViewItem)this.view.ItemContainerGenerator.ContainerFromIndex(0);
  </code></pre>
  
  <p>I am struggling to get the treeviewitems with in the parent treeviewitem (item).</p>
  
  <p>I tried ItemContainerGenerator but the following returns null.</p>
  
  <pre><code>  TreeViewItem child = (TreeViewItem)item.ItemContainerGenerator.ContainerFromIndex(0);
  </code></pre>
  
  <p>As i traversed the treeviewitem using VisualTreeHelper.GetChild() method i ended up with a null return from <strong>ItemsPresenter</strong> that contains the child treeviewitems.</p>
  
  <p>Is there any way i could access the child treeviewitems with in a treeviewitem ?</p>
  
  <p>The xaml code of the template:</p>
  
  <pre><code>  &lt;DataTemplate x:Key="Navigation_SubDataTemplate"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;ContentControl Focusable="False"  Background="{DynamicResource navigationlistboxfont}" Template="{Binding MenuTemplate}" Margin="5" Width="16" Height="16"/&gt;
              &lt;TextBlock Text="{Binding Path=MenuName}" ToolTip="{Binding ToolTip}" FontFamily="Segoe UI Light" FontSize="16" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  
      &lt;HierarchicalDataTemplate x:Key="Navigation_DataTemplate" ItemsSource="{Binding SubMenuItems}" ItemTemplate="{StaticResource Navigation_SubDataTemplate}"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;ContentControl Focusable="False" Background="{DynamicResource navigationlistboxfont}" Template="{Binding MenuTemplate}" Margin="5" Width="20" Height="20"/&gt;
              &lt;TextBlock Margin="4,2" Text="{Binding Path=MenuName}" ToolTip="{Binding ToolTip}" FontFamily="Segoe UI Light" FontSize="16" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
              &lt;/TextBlock&gt;
          &lt;/StackPanel&gt;
      &lt;/HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p>And my treeview</p>
  
  <pre><code>&lt;TreeView x:Name="MytreeView" helper:TreeViewExtension.SelectedItem="{Binding ViewSelected, Mode=TwoWay}"  ItemsSource="{Binding ViewMenuItems}" ItemTemplate="{DynamicResource Navigation_DataTemplate}"  Background="{x:Null}" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Margin="0" ItemContainerStyle="{DynamicResource TreeViewItemStyle1}"  /&gt;
  </code></pre>
  
  <p>Here is my c# code of viewmenuitem used to bind to treeview</p>
  
  <pre><code> List&lt;ViewMenu&gt; subMenus = new List&lt;ViewMenu&gt;();
   subMenus.Add(new ViewMenu() { MenuName = "Child1" });
   subMenus.Add(new ViewMenu() { MenuName = "Child2" });
  
   ViewMenuItems = new ObservableCollection&lt;ViewMenu&gt;();
   ViewMenuItems.Add(new ViewMenu() { MenuName = "Parent", SubMenuItems = subMenus });
  </code></pre>