Edit: I guess the question wasn't stated very clearly. It actually composes of 4 separate ones:
- How does a
TextBlock
get its default color, if the client app doesn't provide any style, either programmatically or through xaml? - How does a
Label
get its default color? - How does a
TextBlock
get its default font size and font family, if the client app doesn't provide any style, either programmatically or through xaml? - How does a
Label
get its default font size and font family?
BTW, the questions are not about how to change or define styles for the color/font size/font family of a TextBlock
or a Label
, although they are somehow related. I think I already knew the answer for #2, that is a Label
gets its color from SystemColors.ControlTextBrushKey and by overriding ConrolTextBrushKey
like so:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red"/>
You would be able to "globally" change color for Label
s. After some research, I guess I also find the answer for #1: A TextBlock
inherits the its foreground color from its containing Window
, which by default gets its Foreground
color from SystemColors.WindowTextBrushKey. By defining a color for the WindowTextBrush like so:
<Window.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowTextBrushKey}"
Color="Yellow"/>
</Window.Resources>
You would be able to change the "foreground" color for the TextBlock
s inside the Window
.
Question #3 and #4 remain puzzles for me, but I am assuming they have to do with the SystemFonts.
Hope this makes sense. I really like to know the answers as they have been bothering me for a while. Many thanks!
Below is the original post:
If you look into the style for a Label
in the theme (for example "aero.normalcolor.xaml") that comes with Windows, you can find
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
Which sets the color for a Label
. But there is no where the FontSize
property is specified in the style, which I assume has something to do with the SystemFonts. For a TextBlock
, it looks even more mysterious as the style for it in "aero.normalcolor.xaml" has only 4 lines:
<Style x:Key="{x:Type TextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping"
Value="NoWrap"/>
<Setter Property="TextTrimming"
Value="None"/>
</Style>
Where does a Label
or a TextBlock
get the values for its color and font size/family from, if the app doesn't set any, and where are those hooks in WPF?
Edit:
This is a test drive attempting to set the TextBlock
color through SystemColors.ControlTextBrush
(assuming that's where a TextBlock
gets its default color from, which seems to be false):
<Window x:Class="TestFontColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red"/>
</StackPanel.Resources>
<Button Content="This is red."/>
<Label Content="This is blue.">
<Label.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Blue"/>
</Label.Resources>
</Label>
<TextBlock Text="TextBlock: This is still black."/>
</StackPanel>