title: WPF TreeView: How to style selected items with rounded corners like in Explorer
body: <p>The selected item in a WPF TreeView has a dark blue background with "sharp" corners. That looks a bit dated today:</p>
  
  <p><img src="https://i.stack.imgur.com/0xrvQ.png" alt="WPF selected TreeViewItem with or without focus"></p>
  
  <p>I would like to change the background to look like in Explorer of Windows 7 (with/without focus):</p>
  
  <p><img src="https://i.stack.imgur.com/hoWO1.png" alt="Explorer TreeViewItem selected"> <img src="https://i.stack.imgur.com/RoY6z.png" alt="Explorer TreeViewItem selected without focus"></p>
  
  <p>What I tried so far does not remove the original dark blue background but paints a rounded border on top of it so that you see the dark blue color at the edges and at the left side - ugly.</p>
  
  <p><img src="https://i.stack.imgur.com/gR9KO.png" alt="enter image description here"></p>
  
  <p>Interestingly, when my version does not have the focus, it looks pretty OK:</p>
  
  <p><img src="https://i.stack.imgur.com/bmJ8O.png" alt="enter image description here"></p>
  
  <p>I would like to refrain from redefining the control template as <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1551151-69e6-4376-900d-a763c9656267/">shown</a> here or <a href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/358b4e2d-1ddc-4504-b9cb-a2abd8495bb5">here</a>. I want to set the minimum required properties to make the selected item look like in Explorer.</p>
  
  <p><strong>Alternative:</strong> I would also be happy to have the focused selected item look like mine does now when it does not have the focus. When losing the focus, the color should change from blue to grey.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;TreeView 
      x:Name="TreeView"
      ItemsSource="{Binding TopLevelNodes}" 
      VirtualizingStackPanel.IsVirtualizing="True"
      VirtualizingStackPanel.VirtualizationMode="Recycling"&gt;
  
      &lt;TreeView.ItemContainerStyle&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;Style.Triggers&gt;
                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                      &lt;Setter Property="BorderBrush" Value="#FF7DA2CE" /&gt;
                      &lt;Setter Property="Background" Value="#FFCCE2FC" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
  
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type viewmodels:ObjectBaseViewModel}" ItemsSource="{Binding Children}"&gt;
              &lt;Border Name="ItemBorder" CornerRadius="2" Background="{Binding Background, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"
                        BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType=TreeViewItem}}" BorderThickness="1"&gt;
                  &lt;StackPanel Orientation="Horizontal" Margin="2"&gt;
                      &lt;Image Name="icon" Source="/ExplorerTreeView/Images/folder.png"/&gt;
                      &lt;TextBlock Text="{Binding Name}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>With the excellent answers of Sheridan and Meleak my TreeView now looks like this in code (a result I am very happy with and which is pretty near Explorer's style):</p>
  
  <pre><code>&lt;TreeView 
  ...
      &lt;TreeView.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;!-- Style for the selected item --&gt;
              &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;Style.Triggers&gt;
                  &lt;!-- Selected and has focus --&gt;
                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                      &lt;Setter Property="BorderBrush" Value="#7DA2CE"/&gt;
                  &lt;/Trigger&gt;
                  &lt;!-- Mouse over --&gt;
                  &lt;Trigger Property="helpers:TreeView_IsMouseDirectlyOverItem.IsMouseDirectlyOverItem" Value="True"&gt;
                      &lt;Setter Property="Background"&gt;
                          &lt;Setter.Value&gt;
                              &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                                  &lt;GradientStop Color="#FFFAFBFD" Offset="0"/&gt;
                                  &lt;GradientStop Color="#FFEBF3FD" Offset="1"/&gt;
                              &lt;/LinearGradientBrush&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                      &lt;Setter Property="BorderBrush" Value="#B8D6FB"/&gt;
                  &lt;/Trigger&gt;
                  &lt;!-- Selected but does not have the focus --&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="BorderBrush" Value="#D9D9D9"/&gt;
                  &lt;/MultiTrigger&gt;
              &lt;/Style.Triggers&gt;
              &lt;Style.Resources&gt;
                  &lt;Style TargetType="Border"&gt;
                      &lt;Setter Property="CornerRadius" Value="2"/&gt;
                  &lt;/Style&gt;
              &lt;/Style.Resources&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
  
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type viewmodels:ObjectBaseViewModel}" ItemsSource="{Binding Children}"&gt;
              &lt;StackPanel Orientation="Horizontal" Margin="2,1,5,2"&gt;
                  &lt;Grid Margin="0,0,3,0"&gt;
                      &lt;Image Name="icon" Source="/ExplorerTreeView/Images/folder.png"/&gt;
                  &lt;/Grid&gt;
                  &lt;TextBlock Text="{Binding Name}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
          &lt;!-- Brushes for the selected item --&gt;
          &lt;LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0"&gt;
              &lt;GradientStop Color="#FFDCEBFC" Offset="0"/&gt;
              &lt;GradientStop Color="#FFC1DBFC" Offset="1"/&gt;
          &lt;/LinearGradientBrush&gt;
          &lt;LinearGradientBrush x:Key="{x:Static SystemColors.ControlBrushKey}" EndPoint="0,1" StartPoint="0,0"&gt;
              &lt;GradientStop Color="#FFF8F8F8" Offset="0"/&gt;
              &lt;GradientStop Color="#FFE5E5E5" Offset="1"/&gt;
          &lt;/LinearGradientBrush&gt;
          &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /&gt;
          &lt;SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>