Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:23:40+00:00 2026-05-16T18:23:40+00:00

I’m trying to make a ControlTemplate for a GroupBox such that if a TextBlock

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:23:41+00:00Added an answer on May 16, 2026 at 6:23 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.