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 6099641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:17:46+00:00 2026-05-23T13:17:46+00:00

I have a problem with a certain WPF style called HeadText with TargetType =

  • 0

I have a problem with a certain WPF style called HeadText with TargetType = "TextBlock". The style defines Foreground, FontSize and Effect. The first time a TextBlock is shown only the Foreground setter is not fired (text color stays black), FontSize and Effect are applied normally. When I remove the TextBlock from parent and return it back, then the foreground is also changed as it should.

Situation:

Presenter.dll assembly

  • class Presenter: Window, loads and displays my usercontrols.
  • Generic.xaml – Resource dictionary that contains styles.
  • Presenter.dll does not directly reference TestPresentable.dll.

TestPresentable.dll assembly

  • TestPresentable: UserControl, has a styled TextBlock.
  • TestPresentable.dll does not directly reference Presenter.dll.

MainApp.exe

  • references both previous assemblies,
  • instantiates MainWindow from Presenter.dll assembly,
  • instantiates TestPresentable from TestPresentable assembly,
  • sets MainWindow.ContentHost.Content = testPresentable

Relevant code:

Presenter.dll

// Themes/Generic.xaml
...
<Style TargetType="{x:Type TextBlock}" x:Key="HeadText">
    <Setter Property="Foreground" Value="#FFFFFFFF" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect ShadowDepth="0" Color="#79000000" BlurRadius="3" Opacity="1" />
        </Setter.Value>
    </Setter>
    <Setter Property="FontSize" Value="24"/>
</Style>
...


// MainWindow.xaml
...
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Presenter.dll;component/Themes/Generic.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ContentPresenter Name="ContentHost"/>
</Grid>
...

TestPresentable.dll

// TestPresentable.xaml
...
<TextBlock Text="{Binding SomeData}" Style="{DynamicResource HeadText}"/>
...
  • 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-23T13:17:47+00:00Added an answer on May 23, 2026 at 1:17 pm

    It seems that there is something strange with TextBlock.Foreground in WPF since 3.5, see:

    • WPF: ContentPresenter changing Foreground unexpectedly depending on where styles are located
    • TextBlock foreground being reset to inherited value after dynamic resource from merged dictionary is applied
    • http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56

    I’ve came up with a workaround using EventSetters and some codebehind for a ResourceDictionary. It’s not pretty but will have to do if I want to have my styles independant of the main app. I’ll post it here since it might be useful to someone and I’ll keep the question open if someone post the right (or better) answer.

    Workaround

    In the ResorceDictionary XAML (e.g. Generic.xaml) add a Class property like so:

    <!-- Generic.xaml -->
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Presenter.Themes.Generic">
    

    Then add a codebehind cs file (e.g. Generic.xaml.cs) with the partial class you specified in the Class property of the ResourceDictionary:

    // Generic.xaml.cs
    partial class Generic { }
    

    In the relevant style of the ResourceDictionary add an EventSetter for the Loaded event:

    <!-- Generic.xaml -->
    <Style TargetType="{x:Type TextBlock}" x:Key="HeadText">
        <EventSetter Event="Loaded" Handler="OnHeadTextLoaded"/>
        <Setter .../>
        <Setter .../>
        <Setter .../>
    </Style>
    

    In the Generic.xaml.cs add a handler for the Loaded event and set the desired Foreground

    //Generic.xaml.cs
    public void OnHeadTextLoaded(object sender, EventArgs args)
    {
        var textBlock = sender as TextBlock;
        if (textBlock == null) return;
        textBlock.Foreground = new SolidColorBrush(Colors.White);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have items added to them
using python 2.6.5 and python-ldap 2.3.10, I have the following problem: Under certain circumstances
I have this problem of my app crashing (only when press a certain UIButton)
Warning: This is my first 'real' WPF Application. I'm certain it's a logic error
I have a certain problem with my Makefile. With this command, I can compile
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE

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.