title: Disable selection in TreeView
body: <p>I see there are ways to disable selection in a <code>ListView</code>, but I haven't seen anything for a <code>TreeView</code> or <code>Grid</code>.</p>
  
  <p>My form is quite basic and looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/EooCD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EooCD.png" alt="View of form"></a></p>
  
  <p>Problem is someone can go click on any one of the labels and suddenly it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/A4Pku.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A4Pku.png" alt="View of form after selection"></a></p>
  
  <p>How can I disable this behaviour?</p>
  
  <pre><code>&lt;TreeView Focusable="False"&gt;
      &lt;TreeView.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;Setter Property="Focusable" Value="False"/&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
      &lt;TreeViewItem Focusable="False"&gt;
          &lt;TreeViewItem.Header&gt;Group Header&lt;/TreeViewItem.Header&gt;
          &lt;TreeViewItem.IsExpanded&gt;true&lt;/TreeViewItem.IsExpanded&gt;
          &lt;Grid Focusable="False"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="Auto"&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition Width="Auto"&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Label Content="Message ID"&gt;
                  &lt;Grid.Row&gt;0&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;0&lt;/Grid.Column&gt;
              &lt;/Label&gt;
              &lt;TextBox Text="{Binding Path=GroupHeader.MessageId}" HorizontalAlignment="Stretch"&gt;
                  &lt;Grid.Column&gt;1&lt;/Grid.Column&gt;
                  &lt;Grid.Row&gt;0&lt;/Grid.Row&gt;
              &lt;/TextBox&gt;
              &lt;Label Content="Creation Date"&gt;
                  &lt;Grid.Row&gt;1&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;0&lt;/Grid.Column&gt;
              &lt;/Label&gt;
              &lt;DatePicker SelectedDate="{Binding Path=GroupHeader.CreationDateTime}"&gt;
                  &lt;Grid.Row&gt;1&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;1&lt;/Grid.Column&gt;
              &lt;/DatePicker&gt;
              &lt;Label Content="Instructing Agent"&gt;
                  &lt;Grid.Row&gt;2&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;0&lt;/Grid.Column&gt;
              &lt;/Label&gt;
              &lt;TextBox Text="{Binding Path=GroupHeader.InstructingAgent}"&gt;
                  &lt;Grid.Row&gt;2&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;1&lt;/Grid.Column&gt;
              &lt;/TextBox&gt;
              &lt;Label Content="Instructed Agent"&gt;
                  &lt;Grid.Row&gt;3&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;0&lt;/Grid.Column&gt;
              &lt;/Label&gt;
              &lt;TextBox Text="{Binding Path=GroupHeader.InstructedAgent}"&gt;
                  &lt;Grid.Row&gt;3&lt;/Grid.Row&gt;
                  &lt;Grid.Column&gt;1&lt;/Grid.Column&gt;
              &lt;/TextBox&gt;
          &lt;/Grid&gt;
      &lt;/TreeViewItem&gt;
     &lt;TreeViewItem&gt;
          ...
     &lt;/TreeViewItem&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Using the <code>TreeView.ItemContainerStyle</code> just disables it on the actual item header, not the content within it it seems.</p>
  
  <p>Update: I've tried as the comment from Chris W and the answer about setting the control template suggested, using the following:</p>
  
  <pre><code>&lt;Style x:Key="StretchingTreeViewItemStyle1" TargetType="{x:Type local:StretchingTreeViewItem}"&gt;
    &lt;Setter Property="Background" Value="Transparent"/&gt;
    &lt;Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/&gt;
    &lt;Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, 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"&gt;
      &lt;Setter.Value&gt;
        &lt;Style&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;/Setter.Value&gt;
    &lt;/Setter&gt;
    &lt;Setter Property="Template"&gt;
      &lt;Setter.Value&gt;
        &lt;ControlTemplate TargetType="{x:Type local:StretchingTreeViewItem}"&gt;
          &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition MinWidth="19" 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" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"&gt;
              &lt;ToggleButton.Style&gt;
                &lt;Style TargetType="{x:Type ToggleButton}"&gt;
                  &lt;Setter Property="Focusable" Value="False"/&gt;
                  &lt;Setter Property="Width" Value="16"/&gt;
                  &lt;Setter Property="Height" Value="16"/&gt;
                  &lt;Setter Property="Template"&gt;
                    &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                        &lt;Border Background="Transparent" Height="16" Padding="5" Width="16"&gt;
                          &lt;Path x:Name="ExpandPath" Data="M0,0 L0,6 L6,0 z" Fill="Transparent" Stroke="#FF989898"&gt;
                            &lt;Path.RenderTransform&gt;
                              &lt;RotateTransform Angle="135" CenterY="3" CenterX="3"/&gt;
                            &lt;/Path.RenderTransform&gt;
                          &lt;/Path&gt;
                        &lt;/Border&gt;
                        &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsChecked" Value="True"&gt;
                            &lt;Setter Property="RenderTransform" TargetName="ExpandPath"&gt;
                              &lt;Setter.Value&gt;
                                &lt;RotateTransform Angle="180" CenterY="3" CenterX="3"/&gt;
                              &lt;/Setter.Value&gt;
                            &lt;/Setter&gt;
                            &lt;Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/&gt;
                            &lt;Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/&gt;
                          &lt;/Trigger&gt;
                          &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                            &lt;Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/&gt;
                            &lt;Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/&gt;
                          &lt;/Trigger&gt;
                          &lt;MultiTrigger&gt;
                            &lt;MultiTrigger.Conditions&gt;
                              &lt;Condition Property="IsMouseOver" Value="True"/&gt;
                              &lt;Condition Property="IsChecked" Value="True"/&gt;
                            &lt;/MultiTrigger.Conditions&gt;
                            &lt;Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/&gt;
                            &lt;Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/&gt;
                          &lt;/MultiTrigger&gt;
                        &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                    &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
                &lt;/Style&gt;
              &lt;/ToggleButton.Style&gt;
            &lt;/ToggleButton&gt;
            &lt;Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"&gt;
              &lt;ContentPresenter Focusable="False" x:Name="PART_Header" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
            &lt;/Border&gt;
            &lt;ItemsPresenter Focusable="False" x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" 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;!--&lt;Trigger Property="IsSelected" Value="True"&gt;
              &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt;
              &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/&gt;
            &lt;/Trigger&gt;--&gt;
            &lt;!--&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.InactiveSelectionHighlightBrushKey}}"/&gt;
              &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/&gt;
            &lt;/MultiTrigger&gt;--&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="VirtualizingPanel.IsVirtualizing" Value="True"&gt;
        &lt;Setter Property="ItemsPanel"&gt;
          &lt;Setter.Value&gt;
            &lt;ItemsPanelTemplate&gt;
              &lt;VirtualizingStackPanel IsItemsHost="True"/&gt;
            &lt;/ItemsPanelTemplate&gt;
          &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
      &lt;/Trigger&gt;
    &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>The result is the text on the <code>TreeViewItem</code> itself is no longer selectable/doesn't get highlighted, but the contents within it still can be.</p>
  
  <p>I've tried setting everything I can think of to <code>Focusable="False"</code> but that doesn't work either, so I am starting to think it isn't related to focus on an item. Especially since if you have nested <code>TreeViews</code> you can have both an inner and an outer item selected at the same time.</p>