语言

TextBlock.Foreground 属性

定义

获取或设置要应用于 TextBlock 的文本内容的 Brush。

public:
 property Brush ^ Foreground { Brush ^ get(); void set(Brush ^ value); };
Brush Foreground();

void Foreground(Brush value);
public Brush Foreground { get; set; }
var brush = textBlock.foreground;
textBlock.foreground = brush;
Public Property Foreground As Brush
<TextBlock Foreground="{StaticResource resourceName}"/>
- or -
<TextBlock Foreground="colorString"/>
- or -
<TextBlock>
  <TextBlock.Foreground>singleBrush</TextBlock.Foreground>
</TextBlock>

属性值

用于应用于文本内容的画笔。 默认值是纯代码透视的 null 画笔,但默认文本样式设置为 黑色 (对于 浅 色主题)或 白色 (对于 深色 主题),表示 UI 中的 TextBlock 元素。

示例

以下 XAML 示例演示如何使用内联定义的属性值“Maroon”将前台属性设置为纯色。 XAML 分析程序使用此“Maroon”值来引用命名的颜色 颜色.Maroon,并创建提供运行时值的 SolidColorBrush 实例。

<TextBlock
  FontSize="32"
  FontWeight="Bold"
  Foreground="Maroon">
  Maroon
</TextBlock>

注解

TextBlock 不是控件,因此不使用模板来更改其外观。 可以改用样式。 对 TextBlock 外观使用命名样式比为每个 TextBlock 实例设置内联属性更为常见(也是更好的做法),尤其是在许多元素中使用通用值时。 有关样式的详细信息,请参阅 XAML 样式。

文本和文本样式的默认外观在很大程度上取决于活动主题和其他设置。 使用默认系统设置或用户首选项的主题和各种样式和行为是 TextBlock 前景色和其他文本特征的有效运行时外观的来源。 可以通过更改属性值或将其他样式应用于特定 TextBlock 实例来更改这些默认值。 可以通过重写 App.xaml 中名为 DefaultTextForegroundThemeBrush 的资源来更改所有默认文本的前景值。

适用于

另请参阅