如何在 WPF 应用程序中设置 TextBlock 和 Label 的默认颜色、字体系列和字体大小?

发布于 2024-12-21 04:58:04 字数 3447 浏览 2 评论 0原文

编辑:我想这个问题没有说得很清楚。它实际上由 4 个独立的部分组成:

  1. 如果客户端应用程序不以编程方式或通过 xaml 提供任何样式,TextBlock 如何获取其默认颜色?
  2. Label 如何获取其默认颜色?
  3. 如果客户端应用程序不以编程方式或通过 xaml 提供任何样式,TextBlock 如何获取其默认字体大小和字体系列?
  4. Label 如何获取其默认字体大小和字体系列?

顺便说一句,问题不是关于如何更改或定义 TextBlockLabel 的颜色/字体大小/字体系列的样式,尽管它们在某种程度上是相关的。我想我已经知道#2 的答案,即 LabelSystemColors.ControlTextBrushKey 并通过重写 ConrolTextBrushKey 如下:

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

您将能够“全局”更改标签的颜色。经过一番研究,我想我也找到了#1的答案:TextBlock从其包含的Window继承其前景色,默认情况下它获取其Foreground 颜色来自 SystemColors.WindowTextBrushKey。通过为 WindowTextBrush 定义颜色,如下所示:

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

您将能够更改 WindowTextBlock 的“前景色”颜色。

问题 #3 和 #4 对我来说仍然是个谜,但我假设它们与 系统字体

希望这是有道理的。我真的很想知道答案,因为它们已经困扰我一段时间了。非常感谢!

以下是原始帖子:


如果您查看 Windows 附带的主题中的 Label 样式(例如“aero.normalcolor.xaml”),您可以找到

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

Which set the color for a 标签。但是样式中没有指定 FontSize 属性,我认为这与 系统字体。对于 TextBlock 来说,它看起来更加神秘,因为它在“aero.normalcolor.xaml”中的样式只有 4 行:

<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 会从中获取其颜色和字体大小/系列的值,WPF 中的这些挂钩在哪里?

编辑:

这是一个尝试通过 SystemColors.ControlTextBrush 设置 TextBlock 颜色的测试(假设 TextBlock 从中获取默认颜色,这似乎是错误的):

<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>

Edit: I guess the question wasn't stated very clearly. It actually composes of 4 separate ones:

  1. How does a TextBlock get its default color, if the client app doesn't provide any style, either programmatically or through xaml?
  2. How does a Label get its default color?
  3. 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?
  4. 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 Labels. 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 TextBlocks 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2024-12-28 04:58:04

据我记得,在大多数情况下,诸如 TextBlockTextBox 等类以及许多其他类都会从 TextElement.Foreground 属性中获取文本颜色。该属性值在整个可视化树中继承,即您可以在根元素上设置它并让大部分文本更改其颜色。例如:

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

事实上,即使对于标签也是如此:默认样式的设置器只是将 TextElement.Foreground 设置为系统颜色之一。

但是,这仅适用于控件的默认状态。正如 Rachel 所写,改变的状态(例如突出显示)不是继承的,而是从系统颜色中获取的。

更新

FontSizeFontFamily 也是如此。它们是具有附加属性用法的 TextElement 类的属性。他们继承了自己的价值观。一旦您在可视化树项上设置了值,它的所有子项都将获得相同的值。除非他们通过显式属性分配或样式等来覆盖它。

再次强调,文本颜色字体大小和字体系列由 TextElement.ForegroundTextElement.FontSizeTextElement.FontFamily 附加依赖项的值控制特定视觉元素的属性。

某些控件(例如Label)显式地将其Foreground设置为某些画笔。发生这种情况是因为画笔是SystemColors 之一。但并非所有控件都必须如此。其他(TextBlockTextBox 等)不会覆盖属性值,而仅使用启动时评估的一些默认设置。 FontSizeFontFamily 也会发生同样的情况。 您无需将它们设置在任何地方即可使其工作。这就是 WPF 的工作原理。

据说,这些值取决于系统主题。我相信它们是在应用程序启动期间进行评估的。也许它们是可配置的。

更新2

您的新问题的答案:

如果客户端应用程序不以编程方式或通过 xaml 提供任何样式,TextBlock 如何获取其默认颜色?

它从 TextElement.Foreground 附加依赖属性的继承值中获取它。默认情况下,它继承自根视觉元素,而根视觉元素又简单地设置为依赖属性 (Brushes.Black) 的默认值。 另请参阅

标签如何获得默认颜色?

它从 TextElement.Foreground 附加依赖属性的值中获取它。由于其默认样式将其设置为 {DynamicResource {x:Static SystemColors.ControlTextBrushKey},因此它会绑定到系统颜色。

如果客户端应用程序不以编程方式或通过 xaml 提供任何样式,TextBlock 如何获取其默认字体大小和字体系列?

与其文本颜色相同。 MSDN 表示字体大小的默认值为 SystemFonts.MessageFontSize 这取决于系统设置。字体系列以类似的方式从 SystemFonts.MessageFontFamily 确定。
TextElement 静态构造函数中注册依赖项属性后,这两个默认值都会传递给 FrameworkPropertyMetadata 构造函数。

更深入:SystemFonts.MessageFontFamilySystemFonts.MessageFontSize 包装内部 SystemParameters.NonClientMetrics,而这些内部 SystemParameters.NonClientMetrics 又从 WIN32 本机 SystemParametersInfo< 检索/代码> http://msdn.microsoft.com/en-us/library/ms724947。因此,WPF 与所有 Windows UI 内容(例如主题、字体等)紧密集成。

标签如何获取其默认字体大小和字体系列?

TextBlock 相同。 Label 派生自 ContentControl,而 ContentControl 又派生自 ControlControl 类将自身添加为具有相同默认值的 TextElement.FontFamilyTextElement.FontSize 属性的所有者。

另请参阅:

属性值继承

更新 3

您应该了解主要内容理念:价值观是继承的。这意味着它们可以从任何地方、任何控件继承。只有对于某种逻辑树结构,你才能准确地判断出它是从哪一个继承而来。你稍微改变一下——颜色就会改变。有人显式设置属性的值 - 所有子级都将继承该值。因此你的问题没有什么实际意义。但从理解 WPF 的角度来看,它们仍然很有趣。

覆盖默认值

虽然您无法更改 SystemFonts 属性的值(它们是只读的),但您也不必这样做。要更改整个窗口的字体大小和系列,只需将所需的值分配给 Window 上的 TextElement 附加属性:

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

以及所有未显式重写继承的控件将收到设置。对于那些确实覆盖的 - 您将必须覆盖它们的默认样式,或者如果它们对值进行硬编码,甚至将它们丢弃。

同样的方法适用于 TextElement.Foreground(和 Background 等)。

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>

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.

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.

UPDATE

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.

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.

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.

UPDATE 2

Answers to your new questions:

How does a TextBlock get its default color, if the client app doesn't provide any style, either programmatically or through xaml?

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?

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.

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?

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.

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?

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.

See also:

Property Value Inheritance

UPDATE 3

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.

Overriding default values

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.

The same approach works for TextElement.Foreground (and Background and so on).

早茶月光 2024-12-28 04:58:04

默认颜色取自操作系统的设置。

您可以通过创建一个画笔来覆盖它们,该画笔具有引用 SystemColors 画笔键

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

The default colors are pulled from the operating system's settings.

You can overwrite them by creating a brush which has the a key that references a SystemColors brush key

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
初与友歌 2024-12-28 04:58:04

根据此: http://msdn.microsoft.com/en-us/library /ms788718.aspx

默认情况下,WPF 使用 Windows\Fonts 目录中的 GlobalUserInterface.composite 字体。

根据此: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.fontsize(v=vs.95).aspx

一个非负值,指定字体大小(以像素为单位)。默认值为 11。

此外,您可以在 MSDN 站点的各个位置找到存储在不同位置的许多其他默认值: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.fontstyle(v=VS.95).aspx 的样式,其中表示:

请求的字体样式,它是从 FontStyles 属性值之一获取的 FontStyle。默认为正常。

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

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

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

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

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

The requested font style, which is a FontStyle that is obtained from one of the FontStyles property values. The default is Normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文