如何在WPF应用程序中设置TextBlock和Label的默认颜色,字体系列和字体大小? [英] How do the default color, font family and font size for a TextBlock and Label get set in an WPF app?
问题描述
我想这个问题没有说清楚.它实际上由4个单独的部分组成:
I guess the question wasn't stated very clearly. It actually composes of 4 separate ones:
- 如果客户端应用程序没有以编程方式或通过xaml提供任何样式,
TextBlock
如何获得其默认颜色? -
Label
如何获得其默认颜色? - 如果客户端应用程序没有以编程方式或通过xaml提供任何样式,
TextBlock
如何获取其默认字体大小和字体系列? -
Label
如何获取其默认字体大小和字体系列?
- 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?
顺便说一句,问题不是关于如何更改或定义TextBlock
或Label
的颜色/字体大小/字体系列的样式,尽管它们之间存在某种联系.我想我已经知道了#2的答案,那就是Label
从 SystemColors.ControlTextBrushKey 并通过像这样覆盖ConrolTextBrushKey
来实现:
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"/>
您将能够全局"更改Label
的颜色.经过一些研究,我想我也找到了答案#1:TextBlock
从其包含的Window
继承其前景色,默认情况下,它从
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>
您将能够为Window
中的TextBlock
更改前景"颜色.
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!
以下是原始帖子:
如果您查看Windows随附主题中的Label
样式(例如"aero.normalcolor.xaml"),则可以找到
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}}"/>
设置Label
的颜色.但是在样式中没有指定FontSize
属性的位置,我认为这与
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>
Label
或TextBlock
从哪里获取颜色和字体大小/系列的值(如果应用程序未设置任何值),以及WPF中的那些钩子在哪里?
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?
这是一个尝试通过SystemColors.ControlTextBrush
设置TextBlock
颜色的测试驱动器(假设这是TextBlock
从其获取默认颜色的位置,这似乎是错误的):
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>
推荐答案
据我所记得,在大多数情况下,诸如TextBlock
,TextBox
之类的类和许多其他类都采用TextElement.Foreground
属性的文本颜色.属性值在整个可视树中都是继承的,即,您可以在根元素上进行设置,并使大部分文本更改其颜色.例如:
As far as I remember, in most cases classes like TextBlock
, TextBox
and many others take the text color from the TextElement.Foreground
property. The property value is inherited throughout the visual tree, i.e. you may set it on the root element and have most of the text change its color. For example:
<Grid TextElement.Foreground="Red">
<TextBlock Text="test"/>
</Grid>
实际上,即使是标签也是如此:设置器的默认样式只是将TextElement.Foreground
设置为系统颜色之一.
In fact, the same is true even for labels: the setter in their default style simply sets the TextElement.Foreground
to one of the system colors.
但是,仅对于控件的默认状态,这是正确的.像突出显示一样,改变的状态不是继承的,而是像Rachel所写的那样取自系统颜色.
However, this is true only for the default state of controls. Altered states, like highlighting, are not inherited, but rather taken from the system colors, as Rachel has written.
更新
FontSize
和FontFamily
也是一样.它们是TextElement
类的属性,具有附加的属性用法.他们继承了自己的价值观.在视觉树项目上设置值后,其所有子项都将获得相同的值.除非他们通过显式属性分配或样式等方式覆盖它.
The same is true for FontSize
and FontFamily
. They are properties of the TextElement
class that have attached property usage. They inherit their values. Once you set a value on a visual tree item, all its children will get the same value. Unless they override it either by an explicit property assignment, or by style and so on.
再次,文本颜色字体大小和字体系列由特定视觉元素上附加的TextElement.Foreground
,TextElement.FontSize
和TextElement.FontFamily
依赖项属性的值控制.
Once again, text color font size and font family are governed by the value of TextElement.Foreground
, TextElement.FontSize
and TextElement.FontFamily
attached dependency properties on a specific visual element.
某些控件,例如Label
,将其Foreground
显式设置为某些画笔.碰巧刷是SystemColors
之一.但这并不一定适用于所有控件.其他(TextBlock
,TextBox
等)不覆盖属性值,而仅使用启动时评估的一些默认设置. FontSize
和FontFamily
也会发生同样的情况. 您无需将它们设置在任何位置即可使它们工作.这就是WPF的工作方式.
Some controls, like Label
explicitly set their Foreground
to some brush. It happens so that the brush is one of the SystemColors
. But it doesn't have to be true for all controls. Others (TextBlock
, TextBox
, etc.) don't override the property value and just use some default settings evaluated on startup. The same happens to FontSize
and FontFamily
. You do not need to set them wherever in order for them to work. That's how WPF works.
假定值取决于系统主题.我相信他们在应用启动过程中得到了评估.也许它们是可配置的.
Supposedly, the values depend on the system theme. I believe they are evaluated during the app startup. Perhaps they are configurable.
更新2
回答新问题:
如果客户端应用程序无法以编程方式或通过xaml提供任何样式,则TextBlock如何获得其默认颜色?
How does a TextBlock get its default color, if the client app doesn't provide any style, either programmatically or through xaml?
它从TextElement.Foreground
附加依赖项属性的继承值中获取.默认情况下,它是从根视觉元素继承的,该元素又被简单地设置为依赖项属性的默认值(Brushes.Black
). 另请参见
It takes it from the inherited value of the TextElement.Foreground
attached dependency property. By default it is inherited from the root visual element, which in turn is simply set to the default value of the dependency property (Brushes.Black
). See also
标签如何获得其默认颜色?
How does a Label get its default color?
它从TextElement.Foreground
附加依赖项属性的值中获取.由于其默认样式将其设置为{DynamicResource {x:Static SystemColors.ControlTextBrushKey}
,因此它将绑定到系统颜色.
It takes it from the value of the TextElement.Foreground
attached dependency property. Since its default style sets it to the {DynamicResource {x:Static SystemColors.ControlTextBrushKey}
, it gets bound to the system color.
如果客户端应用程序无法以编程方式或通过xaml提供任何样式,则TextBlock如何获取其默认字体大小和字体系列?
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?
与其文本颜色相同. MSDN表示,对于字体大小的默认值为 SystemFonts.MessageFontSize 取决于系统设置.字体系列是通过 SystemFonts.MessageFontFamily 确定的.
在TextElement
静态构造函数中将依赖项属性注册后,这两个默认值都将传递给FrameworkPropertyMetadata
构造函数.
The same as for its text color. MSDN says that for the default value of the font size is SystemFonts.MessageFontSize which depends on system settings. Font family is determined in similar way from SystemFonts.MessageFontFamily.
Both these default values are passed to the FrameworkPropertyMetadata
constructor upon dependency property registration in the TextElement
static constructor.
深入了解:SystemFonts.MessageFontFamily
和SystemFonts.MessageFontSize
包装内部SystemParameters.NonClientMetrics
,这些内容又从WIN32本机SystemParametersInfo
http://msdn.microsoft.com/en-us/library/ms724947 .因此,WPF与所有Windows UI东西(例如主题,字体等)紧密集成.
Going deeper: SystemFonts.MessageFontFamily
and SystemFonts.MessageFontSize
wrap internal SystemParameters.NonClientMetrics
which in turn are retrieved from the WIN32 native SystemParametersInfo
http://msdn.microsoft.com/en-us/library/ms724947. Thus the WPF is tightly integrated with all Windows UI stuff like themes, fonts, etc.
标签如何获取其默认字体大小和字体系列?
How does a Label get its default font size and font family?
与TextBlock
相同. Label
从ContentControl
派生,而ContentControl
又从Control
派生. Control
类将自己添加为具有相同默认值的TextElement.FontFamily
和TextElement.FontSize
属性的所有者.
The same as for TextBlock
. Label
derives from ContentControl
which in turn derives from Control
. Control
class adds itself as an owner of the TextElement.FontFamily
and TextElement.FontSize
properties with the same default values.
另请参阅:
更新3
您应该了解主要思想:值是继承的.这意味着它们可以从任何控件的任何地方继承.您可以确切地知道它仅从某种逻辑树结构继承而来.您稍作更改-颜色就会更改.有人明确设置了属性的值-所有子级都将继承该值.因此,您的问题几乎没有实际意义.但是从不了解WPF的角度来看,它们仍然很有趣.
You should understand the main idea: the values are inherited. It means they might be inherited from anywhere, from any control. You can tell exactly which one it is inherited from only for a certain logical tree structure. You change it a bit - and the colors change. Someone sets a property's value explicitly - and all children will inherit the value. Therefore your questions make little practival sense. But they are still interesting from the perspective of undestanding the WPF.
覆盖默认值
尽管不能更改SystemFonts
属性的值(它们是只读的),但不必更改.要更改整个窗口的字体大小和字体,只需将所需的值分配给Window
上的TextElement
附加属性:
Although you cannot change the values of the SystemFonts
properties (they are read-only), you don't have to. To change the font size and family for the whole window, simply assign the desired values to the TextElement
attached properties on the Window
:
<Window TextElement.FontSize="20" TextElement.FontFamily="Century Gothic">
..
</Window>
,并且所有未明确重写继承的控件都将接收到设置.对于那些会覆盖的内容-如果对值进行硬编码,则必须覆盖它们的默认样式,甚至将其丢弃.
and all controls that do not explicitly override the inheritance will receive the settings. For those that do override - you'll have to override their default styles or even throw them away if they hard-code the values.
相同的方法适用于TextElement.Foreground
(和Background
等).
The same approach works for TextElement.Foreground
(and Background
and so on).
这篇关于如何在WPF应用程序中设置TextBlock和Label的默认颜色,字体系列和字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!