title: Get TreeViewItem's parent in HierarchicalDataTemplate in WPF
body: <p>I am merging two examples found on the internet. One about <a href="https://stackoverflow.com/questions/664632/highlight-whole-treeviewitem-line-in-wpf">stretched selection styles</a> and one about <a href="http://www.mattlong.com.au/?p=41" rel="nofollow noreferrer">multi-selection in a treeview</a>.</p>
  
  <p>I have everything working, except for the indentations of the treeview. I could give all my code, but that wouldn't solve it.</p>
  
  <p>My problem lies in the following method:</p>
  
  <pre><code>public static class TreeViewItemExtensions
  {
      public static int GetDepth(this TreeViewItem item)
      {
          FrameworkElement elem = item;
          while (elem.Parent != null)
          {
              var tvi = elem.Parent as TreeViewItem;
              if (tvi != null)
                  return tvi.GetDepth() + 1;
              elem = elem.Parent as FrameworkElement;
          }
          return 0;
      }
  }
  </code></pre>
  
  <p>This method tries to retrieve the depth of a treeviewItem in the tree. The problem is: <code>elem.Parent</code> is always <em>null</em>. Which results in depths of 0.</p>
  
  <p>I think this is happening, because I am using an <code>HierarchicalDataTemplate</code>. A part of my xaml looks like this.</p>
  
  <pre><code>&lt;TreeView Name="folderTree"
            ItemsSource="{Binding Folders}"
            SelectedItemChanged="folderTree_SelectedItemChanged"
            HorizontalContentAlignment="Stretch"&gt;
  
      &lt;TreeView.ItemTemplate&gt;
          &lt;HierarchicalDataTemplate ItemsSource="{Binding Folders}"
                                    DataType="{x:Type Model:IFolder}"&gt;
  
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;StackPanel.Style&gt;
                      &lt;Style TargetType="{x:Type StackPanel}"&gt;
                          &lt;Style.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding IsSelected}"
                                           Value="True"&gt;
                                  &lt;Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/StackPanel.Style&gt;
  
                  &lt;Image Source="{Binding Converter={StaticResource iconConverter}}" Height="{Binding ElementName=theFile,Path=ActualHeight}"/&gt;
                  &lt;TextBlock Text="{Binding FileName}" Name="theFile"/&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.ItemTemplate&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>The XAML for my style of the treeview looks like this:</p>
  
  <pre><code>&lt;Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}"&gt;
  
      &lt;!-- leaving some stuff out here --&gt;
  
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type TreeViewItem}"&gt;
  
                  &lt;ControlTemplate.Resources&gt;
                      &lt;local:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" /&gt;
                  &lt;/ControlTemplate.Resources&gt;
  
                  &lt;StackPanel&gt;
                      &lt;!-- The upper part of the TreeViewItem --&gt;
                      &lt;Border Name="Bd" 
                              Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              Padding="{TemplateBinding Padding}"&gt;
  
                          &lt;!-- The margin is what we try to measure, how can we get the parents from the templatedParents? --&gt;
                          &lt;Grid Margin="{Binding Converter={StaticResource lengthConverter}, 
                                                 RelativeSource={RelativeSource TemplatedParent}, 
                                                 Path=.}"&gt;
  
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="19" /&gt;
                                  &lt;ColumnDefinition /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
  
                              &lt;ToggleButton x:Name="Expander" 
                                            Style="{StaticResource ExpandCollapseToggleStyle}"
                                            IsChecked="{Binding Path=IsExpanded, 
                                                        RelativeSource={RelativeSource TemplatedParent}}"
                                            ClickMode="Press"/&gt;
  
                              &lt;ContentPresenter x:Name="PART_Header"
                                                Grid.Column="1"
                                                ContentSource="Header"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/&gt;
                          &lt;/Grid&gt;
                      &lt;/Border&gt;
  
                      &lt;!-- the children of the TreeViewItem --&gt;
                      &lt;ItemsPresenter x:Name="ItemsHost" /&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;!-- leaving some stuff out here with triggers --&gt;
  
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <blockquote>
    <p>How can I make the HierarchicalDataTemplate fill the <code>Parent</code> property?</p>
  </blockquote>