Splinti Splinti - 1 year ago 106
C# Question

default textblock styling

I have the following checkbox

<CheckBox x:Name="checkBox" Content="CheckBox" Width="74"/>

and I have a button

<Button Name="TestButton" Content="Test" />

I want to set a "default" color for the textblock. I achieve that by having a resourcedictionary which has the following content:

<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White"/>

The problem is, that the Button should still have a black Textblock foreground, but although in another sourcedictionary i have the following, it still changes to white:

<Style TargetType="Button">
<Setter Property="Template">
<ControlTemplate TargetType="Button">
<Border BorderThickness="1,0,0,1" CornerRadius="5" Background="{TemplateBinding Background}">

The ResourceDictionaries are defined in Application.xaml like this:

<ResourceDictionary Source="TextBlock.xaml"/>
<ResourceDictionary Source="Buttons.xaml"/>

Answer Source

You could try overriding the default TextBlock style locally for the ContentPresenter by defining another one in its Resources:

<ContentPresenter ... >
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Black" />

But a better way to set a default control text color is like this in App.Resources. TextElement.Foreground will override this on any given individual element.

    x:Key="{x:Static SystemColors.ControlTextBrushKey}" 

If you use that and discard your default TextBlock style, your original ContentPresenter should work as you had it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download