title: WPF GroupBox ControlTemplate : How to apply a Style only to elements in the Header?
body: <p>I'm trying to make a <code>ControlTemplate</code> for a <code>GroupBox</code> such that if a <code>TextBlock</code> is declared in the <code>Header</code> its <code>Background</code> should be set to yellow.</p>
  
  <p>The problem is that, although I define a style for <code>TextBlock</code>s in the <code>ContentPresenter</code> for the <code>Header</code>, it's not applied except on those <code>TextBlock</code>s which are autogenerated by WPF.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>&lt;Window
    x:Class="TestHeaderTemplate.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    SizeToContent="WidthAndHeight"&gt;
    &lt;Window.Resources&gt;    
      &lt;Style
        TargetType="{x:Type GroupBox}"&gt;
        &lt;Setter
          Property="Template"&gt;
          &lt;Setter.Value&gt;
            &lt;ControlTemplate
              TargetType="{x:Type GroupBox}"&gt;            
              &lt;Border
                Margin="{TemplateBinding Margin}"
                BorderBrush="Black"
                BorderThickness="1"&gt;
                &lt;StackPanel&gt;
                  &lt;Border
                    Margin="0,0,0,5"
                    BorderThickness="5"
                    BorderBrush="LightBlue"
                    &gt;
                    &lt;ContentPresenter
                      ContentSource="Header"&gt;
                      &lt;ContentPresenter.Resources&gt;
                        &lt;Style
                          TargetType="{x:Type TextBlock}"&gt;
                          &lt;Setter
                            Property="Background"
                            Value="Yellow" /&gt;
                        &lt;/Style&gt;
                      &lt;/ContentPresenter.Resources&gt;
                    &lt;/ContentPresenter&gt;
                  &lt;/Border&gt;
                  &lt;ContentPresenter
                    ContentSource="Content" /&gt;
                &lt;/StackPanel&gt;
              &lt;/Border&gt;
            &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
      &lt;/Style&gt;
    &lt;/Window.Resources&gt;
    &lt;StackPanel&gt;
      &lt;TextBox
        Text="All TextBoxes in a GroupBox's Header should be yellow, whether declared or autogenerated." /&gt;
      &lt;GroupBox
        x:Name="firstGroupBox"
        Margin="5"
        Header="I am a TextBlock autogenerated by WPF. Since I'm in the Header, I should be yellow."&gt;
          &lt;TextBlock
            Text="I'm a TextBlock declared in the content of the GroupBox. I should NOT be yellow." /&gt;      
      &lt;/GroupBox&gt;
      &lt;GroupBox
        x:Name="secondGroupbox"
        Margin="5"
        &gt;
        &lt;HeaderedContentControl.Header&gt;      
            &lt;TextBlock
              x:Name="notStyledTextBlock"
              Text="I'm a TextBlock declared in the header. I should be yellow since I'm in the header."
              VerticalAlignment="Center" /&gt;          
        &lt;/HeaderedContentControl.Header&gt;
        &lt;TextBlock
          Text="I'm declared in the content so I should not be yellow." /&gt;
      &lt;/GroupBox&gt;
    &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>As you can see if you try, the background of the <code>TextBlock</code> named <code>notStyledTextBlock</code> in the second <code>GroupBox</code> is not yellow, which means the style defined in the resources of the <code>ContentPresenter</code> in the <code>ControlTemplate</code> is not applied.</p>
  
  <p>Surprisingly, the one that was autogenerated by WPF as a container for the header text of the first <code>GroupBox</code> has its background yellow.</p>
  
  <p>What can I do to make it so my style is applied to the <code>notStyledTextBlock</code> <code>TextBlock</code>?</p>