title: How to change the Border Background Color in a ItemContainerStyle programmatically?
body: <p>I have a TreeView and it's own Style and it's own ItemContainerStyle. In the ItemContainerStyle I have a Border with the Name "SelectedRectangle". Now I want to change the Background Color of this "SelectedRectangle" by using this Code (I found it in the Internet):</p>
  
  <pre><code>    Border brd = (Border)lstDbTree.Template.FindName("SelectedRectangle", lstDbTree); //dosnt work - returns 'null'
      brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#7B7A7C")); // Null Pointer Exception
  </code></pre>
  
  <p>I don't know how I can get access to the ItemContainerStyle XAML to manipulate it programmatically.</p>
  
  <p>The Code:</p>
  
  <h2>Shell.xaml</h2>
  
  <pre><code>    &lt;TreeView DockPanel.Dock="Bottom" Name="lstDbTree"
        ...
        ItemContainerStyle="{StaticResource DbTreeItemStyle}"
        ...
      /&gt;
  </code></pre>
  
  <h2>CoreStyles.xaml</h2>
  
  <pre><code>    &lt;Style TargetType="TreeViewItem" x:Key="DbTreeItemStyle"&gt;
        &lt;Setters...&gt;
        &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType="TreeViewItem"&gt;
              ...
                &lt;Border x:Name="SelectedRectangle" BorderBrush="#44ffffff" BorderThickness="1" Grid.Column="1" CornerRadius="1" IsHitTestVisible="False" Opacity="0" Background="#555355"/&gt;
              ...
            &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
        &lt;/Style
  </code></pre>
  
  <h2>Shell.xaml.cs</h2>
  
  <pre><code>    private void ColorB_OnClick(object sender, RoutedEventArgs e)
      {
          Border brd = (Border)lstDbTree.Template.FindName("SelectedRectangle", lstDbTree); //dosnt work
          brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#7B7A7C"));
      }
  </code></pre>
  
  <h2>What I want to do</h2>
  
  <p><img src="https://i.stack.imgur.com/n8wwd.png" alt="enter image description here"></p>
  
  <p>Thank's a lot for any Help.</p>