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

  • Home
  • SEARCH
  • 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 9184205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:59:17+00:00 2026-06-17T18:59:17+00:00

The stuff that does work I need to style controls of a certain type

  • 0

The stuff that does work

I need to style controls of a certain type that are children of a StackPanel. I’m using:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">...</Style>
    </StackPanel.Resources>
    <TextBlock ...>
    ...
</StackPanel>

And this works fine! Each TextBlock looks to the resources of it’s parent (the StackPanel) to find out how it should be styled. It doesn’t matter how far down you nest the TextBlock down a StackPanel… if it doesn’t find a style in its direct parent, it will look at its parent’s parent and so on, until it finds something (in this case, the style that was defined in ).

The stuff that doesn’t work

I ran into a problem when I nested a TextBlock inside a ContentControl, which had a Template (see code below). The ControlTemplate seems to disrupt the way a TextBlock retrieves its style from its parents, grandparents,…

The use of a ControlTemplate effectively seems to knock out cold the TextBlock’s means of finding its rightful style (the one in StackPanel.Resources). When it encounters a ControlTemplate, it stops looking for its style in the resources up the tree, and instead defaults to the style in MergedDictionaries of the Application itself.

<StackPanel Orientation="Vertical" Background="LightGray">
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Green" />
        </Style>
    </StackPanel.Resources>

    <TextBlock Text="plain and simple in stackpanel, green" />
    <ContentControl>
        <TextBlock Text="inside ContentControl, still green" />
    </ContentControl>
    <ContentControl>
        <ContentControl.Template>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <StackPanel Orientation="Vertical">
                    <ContentPresenter />
                    <TextBlock Text="how come this one - placed in the template - is not green?" />
                </StackPanel>
            </ControlTemplate>
        </ContentControl.Template>
        <TextBlock Text="inside ContentControl with a template, this one is green as well" />
    </ContentControl>

</StackPanel>

Is there a way – besides duplicating the Style in StackPanel.Resources to ControlTemplate.Resources – to make the TextBlock inside that ControlTemplate find the defined style?

Thanks…

  • 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-06-17T18:59:18+00:00Added an answer on June 17, 2026 at 6:59 pm

    WPF considers ControlTemplates to be a boundry, and will not apply implicit styles (styles without an x:Key) inside of templates.

    But there is one exception to this rule: anything that inherits from Control will apply implicit styles.

    So you could use a Label instead of a TextBlock, and it would apply the implicit style defined further up your XAML hierarchy, however since TextBlock inherits from FrameworkElement instead of Control, it won’t apply the implicit style automatically and you have to add it manually.

    My most common way to get around this is to add an implicit style in the ControlTemplate.Resources that is BasedOn the existing implicit TextBlock style

        <ControlTemplate.Resources>
            <Style TargetType="{x:Type TextBlock}" 
                   BasedOn="{StaticResource {x:Type TextBlock}}" />
        <ControlTemplate.Resources>
    

    Other common ways of getting around this are:

    • Place the implicit style in <Application.Resources>. Styles placed here will apply to your entire application, regardless of template boundaries. Be careful with this though, as it will apply the style to TextBlocks inside of other controls as well, like Buttons or ComboBoxes

      <Application.Resources>
          <Style TargetType="{x:Type TextBlock}">
              <Setter Property="Foreground" Value="Green" />
          </Style>
      </Application.Resources>
      
    • Use a Label instead of a TextBlock since it’s inherited from Control, so will apply implicit Styles defined outside the ControlTemplate

    • Give the base style an x:Key and use it as the base style for an implicit TextBlock styles inside the ControlTemplate. It’s pretty much the same as the top solution, however it’s used for base styles that have an x:Key attribute

      <Style x:Key="BaseTextBlockStyle" TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Green" />
      </Style>
      
      ...
      
      <ControlTemplate.Resources>
          <Style TargetType="{x:Type TextBlock}" 
              BasedOn="{StaticResource BaseTextBlockStyle}" />
      <ControlTemplate.Resources>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an internal class A that does a lot of stuff and I
I have a stored procedure that does, among other stuff, some inserts in different
I want to develop an application that does a bunch of cool stuff. The
I've built software that connects to a database and does stuff (does not really
I have a PHP stuff that uses call_user_func to create element/objects to a certain
I have some JavaScript code that does some stuff, but at some point I
I have a Win Form application that does some boring accounting stuff and then
We have some stuff that may be exported into various formats. Currently we have
I am doing some server stuff that could potentially fail (not likely) but something
I have text file with some stuff that i would like to put into

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.