title: TreeView Style FullRowSelect and stay selected when focus lost
body: <p>At a TreeView, I have the following Resources to keep the item selected when Focus is lost.</p>
  
  <pre><code>&lt;TreeView.Resources&gt;
                      &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                          &lt;Style.Resources&gt;
                              &lt;!--  SelectedItem with focus  --&gt;
                              &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                               Opacity=".8"
                                               Color="DodgerBlue" /&gt;
                              &lt;!--  SelectedItem without focus  --&gt;
                              &lt;SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                                               Opacity=".3"
                                               Color="DodgerBlue" /&gt;
                          &lt;/Style.Resources&gt;
                      &lt;/Style&gt;
                  &lt;/TreeView.Resources&gt;
  </code></pre>
  
  <p>But I also have a template in a resource dictionary that I use to get a FullRowSelect style. It is from LeeCampbells Article here: <a href="http://leecampbell.blogspot.de/2009/01/horizontal-stretch-on-treeviewitems.html" rel="nofollow">Horizontal stretch on TreeViewItems</a></p>
  
  <pre><code>        &lt;Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="TreeViewItem"&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="TreeViewItem"&gt;
                          &lt;StackPanel&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="Auto" MinWidth="19" /&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;!--
                                      Note that the following do not work, but I believe the top 2 should?!
                                      &lt;ToggleButton IsChecked="{TemplateBinding IsExpanded}" ClickMode="Press" Name="Expander"&gt;
                                      &lt;ToggleButton IsChecked="{TemplateBinding Property=IsExpanded}" ClickMode="Press" Name="Expander"&gt;
                                      &lt;ToggleButton IsChecked="{TemplateBinding Path=IsExpanded}" ClickMode="Press" Name="Expander"&gt;
                                  --&gt;
                                  &lt;ToggleButton Name="Expander"
                                                ClickMode="Press"
                                                IsChecked="{Binding Path=IsExpanded,
                                                                    RelativeSource={RelativeSource TemplatedParent}}"&gt;
                                      &lt;ToggleButton.Style&gt;
                                          &lt;Style TargetType="ToggleButton"&gt;
                                              &lt;Setter Property="UIElement.Focusable" Value="false" /&gt;
                                              &lt;Setter Property="FrameworkElement.Width" Value="16" /&gt;
                                              &lt;Setter Property="FrameworkElement.Height" Value="16" /&gt;
                                              &lt;Setter Property="Control.Template"&gt;
                                                  &lt;Setter.Value&gt;
                                                      &lt;ControlTemplate TargetType="ToggleButton"&gt;
                                                          &lt;Border Width="16"
                                                                  Height="16"
                                                                  Background="#00FFFFFF"
                                                                  Padding="5,5,5,5"&gt;
                                                              &lt;Path Name="ExpandPath"
                                                                    Fill="#00FFFFFF"
                                                                    Stroke="#FF989898"&gt;
                                                                  &lt;Path.Data&gt;
                                                                      &lt;PathGeometry Figures="M0,0L0,6L6,0z" /&gt;
                                                                  &lt;/Path.Data&gt;
                                                                  &lt;Path.RenderTransform&gt;
                                                                      &lt;RotateTransform Angle="135" CenterX="3" CenterY="3" /&gt;
                                                                  &lt;/Path.RenderTransform&gt;
                                                              &lt;/Path&gt;
                                                          &lt;/Border&gt;
                                                          &lt;ControlTemplate.Triggers&gt;
                                                              &lt;Trigger Property="UIElement.IsMouseOver" Value="True"&gt;
                                                                  &lt;Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF1BBBFA" /&gt;
                                                                  &lt;Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#00FFFFFF" /&gt;
                                                              &lt;/Trigger&gt;
                                                              &lt;Trigger Property="ToggleButton.IsChecked" Value="True"&gt;
                                                                  &lt;Setter TargetName="ExpandPath" Property="UIElement.RenderTransform"&gt;
                                                                      &lt;Setter.Value&gt;
                                                                          &lt;RotateTransform Angle="180" CenterX="3" CenterY="3" /&gt;
                                                                      &lt;/Setter.Value&gt;
                                                                  &lt;/Setter&gt;
                                                                  &lt;Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#FF595959" /&gt;
                                                                  &lt;Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF262626" /&gt;
                                                              &lt;/Trigger&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"
                                          Grid.Column="1"
                                          HorizontalAlignment="Stretch"
                                          Background="{TemplateBinding Panel.Background}"
                                          BorderBrush="{TemplateBinding Border.BorderBrush}"
                                          BorderThickness="{TemplateBinding Border.BorderThickness}"
                                          Padding="{TemplateBinding Control.Padding}"
                                          SnapsToDevicePixels="True"&gt;
                                      &lt;ContentPresenter x:Name="PART_Header"
                                                        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                                        Content="{TemplateBinding HeaderedContentControl.Header}"
                                                        ContentSource="Header"
                                                        ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                                                        ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                                        ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
                                                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /&gt;
                                  &lt;/Border&gt;
                                  &lt;ItemsPresenter x:Name="ItemsHost"
                                                  Grid.Row="1"
                                                  Grid.Column="1" /&gt;
                              &lt;/Grid&gt;
                          &lt;/StackPanel&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="TreeViewItem.IsExpanded" Value="False"&gt;
                                  &lt;Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="Collapsed" /&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="ItemsControl.HasItems" Value="False"&gt;
                                  &lt;Setter TargetName="Expander" Property="UIElement.Visibility" Value="Hidden" /&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="TreeViewItem.IsSelected" Value="True"&gt;
                                  &lt;Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /&gt;
                                  &lt;Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /&gt;
                              &lt;/Trigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="TreeViewItem.IsSelected" Value="True" /&gt;
                                      &lt;Condition Property="Selector.IsSelectionActive" Value="False" /&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /&gt;
                                  &lt;Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;Trigger Property="UIElement.IsEnabled" Value="False"&gt;
                                  &lt;Setter Property="TextElement.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&gt;
  
  
  
  
      &lt;/TreeView.ItemContainerStyle&gt;
  
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>At the moment I do not seem to be able to combine those two. Perhaps it's already too late here.</p>
  
  <p>Where should I put the style in the template to have FullRowSelect and keep the selection when focus is lost?</p>