title: WPF treeview item background over entire row
body: <p>i am writing one app and i need to set background under entire row by items. I found some inspiration in <a href="https://stackoverflow.com/questions/664632/highlight-whole-treeviewitem-line-in-wpf#">here</a> but i cannot get idea how to get Border background value in template (the problem is a bit more complicated:)</p>
  
  <p>My problem is that in the treeview are 2 types of "data" (files and folders). User can modify the background under files and folders. </p>
  
  <p>Right now i have the background on textblock, but it seems horrible and i want to have the background over entire row (i think it will looks much better).</p>
  
  <p>Now it seems so:<br>
  <img src="https://i.stack.imgur.com/8W1Sg.png" alt="img1"></p>
  
  <p>but what i need is:<br>
  <img src="https://i.stack.imgur.com/dTTgq.png" alt="img2"></p>
  
  <p>If i change the value of the border background, i change all items (logically). So i guess i really need to work with the textblock background, but i cannot reach spreading over entire row (stretch is not a solution, because it spread it just to the end of the line but not that white space before).</p>
  
  <p>Thanks for advice.</p>
  
  <p><strong>EDIT :</strong> XAML in here:<br></p>
  
  <pre><code>&lt;ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:treeView"&gt;
  &lt;!-- TREEVIEW  --&gt;
      &lt;Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton"&gt;
          &lt;Setter Property="Focusable" Value="False"/&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="ToggleButton"&gt;
                      &lt;Grid
            Width="15"
            Height="13"
            Background="Transparent"&gt;
                          &lt;!--&lt;Path x:Name="ExpandPath"
              HorizontalAlignment="Left" 
              VerticalAlignment="Center" 
              Margin="1,1,1,1"
              Fill="Red"
              Data="M 4 0 L 8 4 L 4 8 Z"/&gt;--&gt;
                      &lt;/Grid&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsChecked"
                 Value="True"&gt;
                              &lt;!--&lt;Setter Property="Data"
                  TargetName="ExpandPath"
                  Value="M 0 4 L 8 4 L 4 8 Z"/&gt;--&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="TreeViewItemFocusVisual"&gt;
          &lt;Setter Property="Control.Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate&gt;
                      &lt;Border&gt;
                          &lt;Rectangle Margin="0,0,0,0"
                   StrokeThickness="5"
                   Stroke="Black"
                   StrokeDashArray="1 2"
                   Opacity="0"/&gt;
                      &lt;/Border&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 Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
          &lt;Setter Property="VerticalContentAlignment" Value="{Binding Path=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;ControlTemplate.Resources&gt;
                          &lt;local:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" /&gt;
                      &lt;/ControlTemplate.Resources&gt;
                      &lt;StackPanel&gt;
                          &lt;Border x:Name="Bd"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"&gt;
                              &lt;Grid Margin="{Binding Converter={StaticResource lengthConverter}, RelativeSource={RelativeSource TemplatedParent}}"&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="19" /&gt;
                                      &lt;ColumnDefinition/&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;ToggleButton Grid.Column="1" x:Name="Expander"
                                    Style="{StaticResource ExpandCollapseToggleStyle}"
                                    IsChecked="{Binding Path=IsExpanded,
                                                RelativeSource={RelativeSource TemplatedParent}}"
                                    ClickMode="Press"/&gt;
                                  &lt;ContentPresenter x:Name="PART_Header" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ContentSource="Header" /&gt;
                              &lt;/Grid&gt;
                          &lt;/Border&gt;
                          &lt;ItemsPresenter x:Name="ItemsHost" /&gt;
                      &lt;/StackPanel&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsExpanded" Value="false"&gt;
                              &lt;Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/&gt;
                          &lt;/Trigger&gt;
                          &lt;Trigger Property="HasItems" Value="false"&gt;
                              &lt;Setter TargetName="Expander" Property="Visibility" Value="Hidden"/&gt;
                          &lt;/Trigger&gt;
                          &lt;MultiTrigger&gt;
                              &lt;MultiTrigger.Conditions&gt;
                                  &lt;Condition Property="HasHeader" Value="false"/&gt;
                                  &lt;Condition Property="Width" Value="Auto"/&gt;
                              &lt;/MultiTrigger.Conditions&gt;
                              &lt;Setter TargetName="PART_Header" Property="MinWidth" Value="75"/&gt;
                          &lt;/MultiTrigger&gt;
                          &lt;MultiTrigger&gt;
                              &lt;MultiTrigger.Conditions&gt;
                                  &lt;Condition Property="HasHeader" Value="false"/&gt;
                                  &lt;Condition Property="Height" Value="Auto"/&gt;
                              &lt;/MultiTrigger.Conditions&gt;
                              &lt;Setter TargetName="PART_Header" Property="MinHeight" Value="19"/&gt;
                          &lt;/MultiTrigger&gt;
                          &lt;Trigger Property="IsSelected" Value="true"&gt;
                              &lt;Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt;
                              &lt;Setter Property="Foreground" 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 TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt;
                              &lt;Setter Property="Foreground" 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&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>