3

I'm trying to make a ControlTemplate for a GroupBox such that if a TextBlock is declared in the Header its Background should be set to yellow.

The problem is that, although I define a style for TextBlocks in the ContentPresenter for the Header, it's not applied except on those TextBlocks which are autogenerated by WPF.

Here is the code :

<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">
  <Window.Resources>    
    <Style
      TargetType="{x:Type GroupBox}">
      <Setter
        Property="Template">
        <Setter.Value>
          <ControlTemplate
            TargetType="{x:Type GroupBox}">            
            <Border
              Margin="{TemplateBinding Margin}"
              BorderBrush="Black"
              BorderThickness="1">
              <StackPanel>
                <Border
                  Margin="0,0,0,5"
                  BorderThickness="5"
                  BorderBrush="LightBlue"
                  >
                  <ContentPresenter
                    ContentSource="Header">
                    <ContentPresenter.Resources>
                      <Style
                        TargetType="{x:Type TextBlock}">
                        <Setter
                          Property="Background"
                          Value="Yellow" />
                      </Style>
                    </ContentPresenter.Resources>
                  </ContentPresenter>
                </Border>
                <ContentPresenter
                  ContentSource="Content" />
              </StackPanel>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <StackPanel>
    <TextBox
      Text="All TextBoxes in a GroupBox's Header should be yellow, whether declared or autogenerated." />
    <GroupBox
      x:Name="firstGroupBox"
      Margin="5"
      Header="I am a TextBlock autogenerated by WPF. Since I'm in the Header, I should be yellow.">
        <TextBlock
          Text="I'm a TextBlock declared in the content of the GroupBox. I should NOT be yellow." />      
    </GroupBox>
    <GroupBox
      x:Name="secondGroupbox"
      Margin="5"
      >
      <HeaderedContentControl.Header>      
          <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" />          
      </HeaderedContentControl.Header>
      <TextBlock
        Text="I'm declared in the content so I should not be yellow." />
    </GroupBox>
  </StackPanel>
</Window>

As you can see if you try, the background of the TextBlock named notStyledTextBlock in the second GroupBox is not yellow, which means the style defined in the resources of the ContentPresenter in the ControlTemplate is not applied.

Surprisingly, the one that was autogenerated by WPF as a container for the header text of the first GroupBox has its background yellow.

What can I do to make it so my style is applied to the notStyledTextBlock TextBlock?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Pequatre
  • 33
  • 1
  • 3
  • Setting the background of the Border in your style to Yellow would fix it in your example, but would add a yellow background to non textblocks used in headers as well. I upvoted HCL's answer since I believe the explanation he was given, it is by design. – Wallstreet Programmer Sep 09 '10 at 13:24

1 Answers1

2

I had also problems with GroupBoxes and ContentPresenter. I have posted a question and because no answer was given I investigated myself a little. Look at this answer, maybe its the same issue (Additional info: I have not posted my real problem-code but a simplified example that could be used to reproduce).

Community
  • 1
  • 1
HCL
  • 36,053
  • 27
  • 163
  • 213
  • You're right it's kind of the same problem : especially this part : "If the Content of ContentControl is already a WPF-Element, it is created before using it in the ContenPresenter" <-- that's probably why the style is not applied to the 2nd TextBlock. Yet I can't see a way to tell WPF that I want some style applied only to the Header. ControlTemplate.Resources doesn't help since the style would be applied to all TextBlocks (both in header and content). I wish there was a ContentPresenter.ContentStyle property... – Pequatre Sep 09 '10 at 12:38
  • I've marked your answer as accepted since I haven't been able to make any progress on the issue. Thanks also to WallStreet Programmer for the support. – Pequatre Sep 14 '10 at 11:36