How to set color, font family and font size for TextBlock and Label in a WPF application?

Edit: I think the question has not been formulated very clearly. In fact, it consists of 4 separate:

  • How does TextBlock get its default color if the client application does not provide any style either programmatically or through xaml?
  • How does Label get its default color?
  • How does TextBlock get the default font size and font family if the client application does not provide any style, either programmatically or through xaml?
  • How does Label get the default font size and font family?

By the way, the questions do not concern how to change or define styles for the TextBlock or Label color / font / font family, although they are somehow related. I think I already knew the answer to # 2, i.e. Label gets its color from SystemColors.ControlTextBrushKey and overriding ConrolTextBrushKey like this:

 <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red"/> 

You can globally change the color for Label s. After some research, I think I also found the answer for # 1: A TextBlock inherits its foreground color from its containing Window , which by default gets its Foreground color from SystemColors.WindowTextBrushKey . Defining a color for WindowTextBrush as follows:

 <Window.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.WindowTextBrushKey}" Color="Yellow"/> </Window.Resources> 

You can change the foreground color for the TextBlock inside the Window .

Question # 3 and # 4 remains a mystery to me, but I assume that they are related to SystemFonts .

Hope this makes sense. I really like to know the answers, as they bother me for a while. Thank you very much!

Below is the original message:


If you look at the Label style in the theme (for example, "aero.normalcolor.xaml") that comes with Windows, you can find

 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 

What sets the color for the Label . But there is no FontSize property in the style that I believe is relevant to SystemFonts . For TextBlock it looks even more mysterious, since 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 Label or TextBlock get values โ€‹โ€‹for their color and font size / family / family if the application is not installed, and where are these interceptors in WPF?

Edit:

This is a test drive trying to set the TextBlock color through SystemColors.ControlTextBrush (assuming where the TextBlock gets its default color, which seems 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> 

+4
source share
3 answers

As far as I remember, in most cases, classes such as TextBlock , TextBox and many others take the color of the text from the TextElement.Foreground property. The property value is inherited throughout the visual tree, i.e. You can set it to the root element, and most of the text will change its color. For instance:

 <Grid TextElement.Foreground="Red"> <TextBlock Text="test"/> </Grid> 

In fact, the same is true for labels: the default style setter simply sets the TextElement.Foreground to one of the system colors.

However, this is only true for the default state of controls. Altered states, such as highlighting, are not inherited, but rather are taken from system colors, as Rachel wrote.

UPDATE

The same is true for FontSize and FontFamily . They are properties of the TextElement class that attached the use property. They inherit their values. When you set a value in a visual tree element, all its children will receive the same value. If they do not redefine it either by explicit assignment of properties, or by style, etc.

Again, the font color and font family are determined by the TextElement.Foreground , TextElement.FontSize and TextElement.FontFamily property TextElement.FontFamily related dependency properties for a particular visual element.

Some controls, such as Label , explicitly set their Foreground to some brush. It happens so that the brush is one of SystemColors . But this is not necessary for all controls. Others ( TextBlock , TextBox , etc.) do not override the value of the property and simply use some default settings evaluated at startup. The same thing happens with FontSize and FontFamily . You do not need to install them wherever they can work . How does WPF work?

Presumably, the values โ€‹โ€‹depend on the system theme. I believe that they are evaluated at application launch time. Perhaps they are customizable.

UPDATE 2

Answers your new questions:

How does TextBlock get its default color if the client application does not provide any style either programmatically or through xaml?

It takes it from the inherited value of the TextElement.Foreground binding TextElement.Foreground . By default, it inherits from the root visual element, which, in turn, is simply set to the default value for the dependency property ( Brushes.Black ). see also

How does Label get its default color?

It takes it from the value of the bound dependency property of TextElement.Foreground . Since its default style sets it to {DynamicResource {x:Static SystemColors.ControlTextBrushKey} , it binds to the color of the system.

How does TextBlock get its default font size and font family if the client application does not provide any style either programmatically or through xaml?

Same as text color. MSDN says that the default font size is SystemFonts.MessageFontSize , which depends on the system settings. A font family is defined in the same way from SystemFonts.MessageFontFamily . Both of these default values โ€‹โ€‹are passed to the FrameworkPropertyMetadata constructor when the dependency property is registered in the static TextElement constructor.

Next: SystemFonts.MessageFontFamily and SystemFonts.MessageFontSize wrap the internal SystemParameters.NonClientMetrics , which, in turn, is extracted from the WIN32 SystemParametersInfo source file http://msdn.microsoft.com/en-us/library/ms724947 . Thus, WPF is tightly integrated with all Windows UI materials, such as themes, fonts, etc.

How does Label get the default font size and font family?

Same as for TextBlock . Label comes from ContentControl , which in turn comes from Control . Control class adds itself as the owner of the TextElement.FontFamily and TextElement.FontSize with the same default values.

See also:

Property Inheritance

UPDATE 3

You need to understand the basic idea: values โ€‹โ€‹are inherited. This means that they can be inherited from anywhere, from any control. You can specify exactly which one is inherited only for a specific logical tree structure. You change it a little - and the colors change. Someone sets the property value explicitly - and all children inherit the value. Therefore, your questions are not very practical. But they are still interesting in terms of the lack of rights to WPF.

Override Defaults

Although you cannot change the values โ€‹โ€‹of the SystemFonts properties (they are read-only), you do not need to. To change the font size and family for the entire window, simply assign the desired TextElement values TextElement attached properties in the Window :

 <Window TextElement.FontSize="20" TextElement.FontFamily="Century Gothic"> .. </Window> 

and all controls that do not explicitly override inheritance will receive the settings. For those who override - you will have to override the default styles or even throw them away if they hardcode the values.

The same approach works for TextElement.Foreground (and Background , etc.).

+6
source

The default colors are pulled from the operating system settings.

You can overwrite them by creating a brush in which there is a key that refers to SystemColors

 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
+2
source

According to this: http://msdn.microsoft.com/en-us/library/ms788718.aspx

By default, WPF uses the GlobalUserInterface.composite font in the Windows \ Fonts directory.

And in accordance with this: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.fontsize(v=vs.95).aspx

A non-negative value that determines the font size measured in pixels. The default value is 11.

In addition, you can find many other default values โ€‹โ€‹stored in different places on the MSDN site: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.fontstyle(v=VS.95 ) .aspx for a style that says:

The requested font style, which is FontStyle, which is obtained from one of the values โ€‹โ€‹of the FontStyles property. The default value is Normal.

0
source

Source: https://habr.com/ru/post/1385956/


All Articles