title: TreeViewItem - Use ControlTemplate and HierarchicalDataTemplate together
body: <p>I'm using HierarchicalDataTemplate in my TreeView, and I wanted to also overwrite the default template for the TreeViewItem so that when an item is selected, it only highlights the text, not including the icon next to it.</p>
  
  <pre><code>&lt;TreeView.ItemTemplate&gt;
       &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
                          &lt;TreeViewItem Style="{StaticResource TreeViewItemStyle}" Header="{Binding DisplayText}" /&gt;
  
        &lt;/HierarchicalDataTemplate&gt;
  &lt;/TreeView.ItemTemplate&gt;
  
  
              &lt;TreeView.Resources&gt;
                  &lt;Style x:Key="TreeViewItemFocusVisual"&gt;
                      &lt;Setter Property="Control.Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate&gt;
                                  &lt;Rectangle/&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
                  &lt;Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}"&gt;
                      &lt;Setter Property="Focusable" Value="False"/&gt;
                      &lt;Setter Property="Width" Value="19"/&gt;
                      &lt;Setter Property="Height" Value="13"/&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                                  &lt;Border Width="19" Height="13" Background="Transparent"&gt;
                                      &lt;Border Width="9" Height="9" SnapsToDevicePixels="true" BorderBrush="#FF7898B5" BorderThickness="1" CornerRadius="1"&gt;
                                          &lt;Border.Background&gt;
                                              &lt;LinearGradientBrush EndPoint="1,1" StartPoint="0,0"&gt;
                                                  &lt;GradientStop Color="White" Offset=".2"/&gt;
                                                  &lt;GradientStop Color="#FFC0B7A6" Offset="1"/&gt;
                                              &lt;/LinearGradientBrush&gt;
                                          &lt;/Border.Background&gt;
                                          &lt;Path x:Name="ExpandPath" Fill="Black" Margin="1,1,1,1" Data="M 0 2 L 0 3 L 2 3 L 2 5 L 3 5 L 3 3 L 5 3 L 5 2 L 3 2 L 3 0 L 2 0 L 2 2 Z"/&gt;
                                      &lt;/Border&gt;
                                  &lt;/Border&gt;
                                  &lt;ControlTemplate.Triggers&gt;
                                      &lt;Trigger Property="IsChecked" Value="True"&gt;
                                          &lt;Setter Property="Data" TargetName="ExpandPath" Value="M 0 2 L 0 3 L 5 3 L 5 2 Z"/&gt;
                                      &lt;/Trigger&gt;
                                  &lt;/ControlTemplate.Triggers&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
                  &lt;Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}"&gt;
                      &lt;Setter Property="Background" Value="Transparent"/&gt;
                      &lt;Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
                      &lt;Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
                      &lt;Setter Property="Padding" Value="1,0,0,0"/&gt;
                      &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
                      &lt;Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type TreeViewItem}"&gt;
                                  &lt;Grid&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition Width="Auto"/&gt;
                                          &lt;ColumnDefinition Width="Auto"/&gt;
                                          &lt;ColumnDefinition Width="*"/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
                                      &lt;Grid.RowDefinitions&gt;
                                          &lt;RowDefinition Height="Auto"/&gt;
                                          &lt;RowDefinition/&gt;
                                      &lt;/Grid.RowDefinitions&gt;
                                      &lt;ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/&gt;
                                      &lt;StackPanel Orientation="Horizontal" Grid.Column="1" &gt;
                                          &lt;Image Width="16" Height="16" Margin="3,0" Source="{Binding Path=ImageSource}" /&gt;
                                          &lt;Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="0" Padding="0"&gt;
                                              &lt;ContentPresenter x:Name="PART_Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header"/&gt;
                                          &lt;/Border&gt;
                                      &lt;/StackPanel&gt;
                                      &lt;ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/&gt;
                                  &lt;/Grid&gt;
                                  &lt;ControlTemplate.Triggers&gt;
                                      &lt;Trigger Property="IsExpanded" Value="false"&gt;
                                          &lt;Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/&gt;
                                      &lt;/Trigger&gt;
                                      &lt;Trigger Property="HasItems" Value="false"&gt;
                                          &lt;Setter Property="Visibility" TargetName="Expander" Value="Hidden"/&gt;
                                      &lt;/Trigger&gt;
                                      &lt;Trigger Property="IsSelected" Value="true"&gt;
                                          &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt;
                                          &lt;Setter Property="Control.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/&gt;
                                      &lt;/Trigger&gt;
                                      &lt;MultiTrigger&gt;
                                          &lt;MultiTrigger.Conditions&gt;
                                              &lt;Condition Property="IsSelected" Value="true"/&gt;
                                              &lt;Condition Property="IsSelectionActive" Value="false"/&gt;
                                          &lt;/MultiTrigger.Conditions&gt;
                                          &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt;
                                          &lt;Setter Property="Control.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
                                      &lt;/MultiTrigger&gt;
                                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                          &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt;
                                      &lt;/Trigger&gt;
                                  &lt;/ControlTemplate.Triggers&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                      &lt;Style.Triggers&gt;
                          &lt;Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true"&gt;
                              &lt;Setter Property="ItemsPanel"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;VirtualizingStackPanel/&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Trigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/TreeView.Resources&gt;
  </code></pre>
  
  <p>I couldn't find a way to use both the HierarchicalDataTemplate and ControlTemplate together so I can specify  the ItemsSource and also change parts of the control's behavior. When I do the above code, it won't select the treeviewitem at all. </p>