title: Hierarchical Datatemplate Binding To DataType Not DataContext
body: <p>I have a usercontrol with a Tree inside it with, the command property binds to the DataType and not the DataContext. </p>
  
  <p>How can I redirect the binding to go to the DataContext and Not the DataType ?  Also Out of curiosity how would I bind to the UserControl's DataContext instead of the Tree's DataContext ? </p>
  
  <p>Here is the code in question:</p>
  
  <pre><code>&lt;HierarchicalDataTemplate 
              DataType="{x:Type viewModel:UsersViewModel}" 
              ItemsSource="{Binding Children}"
              &gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding UserName}"&gt;
                      &lt;TextBlock.ContextMenu&gt;
                              &lt;ContextMenu&gt;
                                  &lt;MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/&gt;
                                  &lt;MenuItem Header="Delete"/&gt;
                              &lt;/ContextMenu&gt;
                          &lt;/TextBlock.ContextMenu&gt;
                  &lt;/TextBlock&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p>It seems to bind to the UsersViewModel and Not the DataContext(AllUsersViewModel).</p>
  
  <p>This is the entire piece of XAML Just in case:</p>
  
  <pre><code> &lt;Grid Width="150"&gt;
  &lt;TreeView ItemsSource="{Binding Users}" DataContext="{Binding allUsersViewModel}"&gt;
      &lt;TreeView.ItemContainerStyle&gt;
          &lt;!-- 
      This Style binds a TreeViewItem to a TreeViewItemViewModel. 
      --&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
              &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
              &lt;Setter Property="FontWeight" Value="Normal" /&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                      &lt;Setter Property="FontWeight" Value="Bold" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
  
      &lt;TreeView.Resources&gt;    
          &lt;ContextMenu x:Key="CategoryMenu"&gt;
              &lt;MenuItem Header="Add Subcategory" Command="New"&gt;
              &lt;/MenuItem&gt;
  
              &lt;MenuItem Header="Remove Category" Command="Delete"&gt;
              &lt;/MenuItem&gt;
          &lt;/ContextMenu&gt;
  
          &lt;HierarchicalDataTemplate 
              DataType="{x:Type viewModel:UsersViewModel}" 
              ItemsSource="{Binding Children}"
              &gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding UserName}"&gt;
                      &lt;TextBlock.ContextMenu&gt;
                              &lt;ContextMenu&gt;
                                  &lt;MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/&gt;
                                  &lt;MenuItem Header="Delete"/&gt;
                              &lt;/ContextMenu&gt;
                          &lt;/TextBlock.ContextMenu&gt;
                  &lt;/TextBlock&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
          &lt;HierarchicalDataTemplate 
              DataType="{x:Type viewModel:PermissionCategoryViewModel}" 
              ItemsSource="{Binding Children}"
              &gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding PermissionCategoryName}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
          &lt;DataTemplate DataType="{x:Type viewModel:PermissionViewModel}"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding PermissionName}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  &lt;/Grid&gt; 
  </code></pre>
  
  <p>Thanks for the Help!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Well I tried a few things, but it didnt work out =(. Nothing Happens</p>
  
  <p>I should have mentioned that my MainWindow Has the DataContext and the UserControl Inherits it by placing the UserControl Inside of it.</p>
  
  <pre><code>&lt;Window.DataContext&gt;
      &lt;viewModel:MainViewModel/&gt;
  &lt;/Window.DataContext&gt;
  </code></pre>
  
  <p>I tried these different things</p>
  
  <pre><code>Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"
  </code></pre>
  
  <p>Lastly</p>
  
  <pre><code>Command="{Binding PlacementTarget.DataContext.EditCommand, 
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
  </code></pre>