TextBlock.Foreground 属性

定义

获取或设置要应用于 Brush 的文本内容的 TextBlockGets or sets the Brush to apply to the text contents of the TextBlock.

public:
 property System::Windows::Media::Brush ^ Foreground { System::Windows::Media::Brush ^ get(); void set(System::Windows::Media::Brush ^ value); };
public System.Windows.Media.Brush Foreground { get; set; }
member this.Foreground : System.Windows.Media.Brush with get, set
Public Property Foreground As Brush

属性值

要应用于文本内容的画笔。The brush used to apply to the text contents. 默认值为 BlackThe default is Black.

示例

下面的示例演示如何设置 TextBlock 元素的 Foreground 特性。The following example shows how to set the Foreground attribute of a TextBlock element.

<TextBlock 
  Name="textBlock"
  
  Background="AntiqueWhite" 
  Foreground="Navy" 
  
  FontFamily="Century Gothic"
  FontSize="12"
  FontStretch="UltraExpanded"
  FontStyle="Italic"
  FontWeight="UltraBold"
  
  LineHeight="Auto"
  Padding="5,10,5,10"
  TextAlignment="Center"
  TextWrapping="Wrap"
  
  Typography.NumeralStyle="OldStyle"
  Typography.SlashedZero="True"
>
  <Run Background="LightGreen">Text run 1.</Run>
  <LineBreak/><Run Background="LightBlue">Text run 2.</Run>
  <LineBreak/><Run Background="LightYellow">Text run 3.</Run>
</TextBlock>

下面的示例演示如何以编程方式设置 Foreground 属性。The following example shows how to set the Foreground property programmatically.

TextBlock textBlock = new TextBlock(new Run("A bit of text content..."));

textBlock.Background              = Brushes.AntiqueWhite;
textBlock.Foreground              = Brushes.Navy;

textBlock.FontFamily              = new FontFamily("Century Gothic");
textBlock.FontSize                = 12;
textBlock.FontStretch             = FontStretches.UltraExpanded;
textBlock.FontStyle               = FontStyles.Italic;
textBlock.FontWeight              = FontWeights.UltraBold;

textBlock.LineHeight              = Double.NaN;
textBlock.Padding                 = new Thickness(5, 10, 5, 10);
textBlock.TextAlignment           = TextAlignment.Center;
textBlock.TextWrapping            = TextWrapping.Wrap;

textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle;
textBlock.Typography.SlashedZero  = true;
Dim textBlock As New TextBlock(New Run("A bit of text content..."))

textBlock.Background = Brushes.AntiqueWhite
textBlock.Foreground = Brushes.Navy

textBlock.FontFamily = New FontFamily("Century Gothic")
textBlock.FontSize = 12
textBlock.FontStretch = FontStretches.UltraExpanded
textBlock.FontStyle = FontStyles.Italic
textBlock.FontWeight = FontWeights.UltraBold

textBlock.LineHeight = Double.NaN
textBlock.Padding = New Thickness(5, 10, 5, 10)
textBlock.TextAlignment = TextAlignment.Center
textBlock.TextWrapping = TextWrapping.Wrap

textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle
textBlock.Typography.SlashedZero = True

注解

有关显示可用预定义画笔颜色的样本表,请参阅 BrushesFor a table of swatches that show the available pre-defined brush colors, see Brushes.

对子元素上的任何 Foreground 设置都将覆盖此顶级设置。Any Foreground settings on child elements override this top-level setting.

此依赖属性还具有附加的属性用法。This dependency property also has an attached property usage. XAMLXAML中,使用情况是 <object TextBlock.Foreground="value".../>的,其中object是包含在 TextBlock内的对象元素(通常是 flow 元素),是解析为 Brush 实现值的字符串。In XAMLXAML, the usage is <object TextBlock.Foreground="value".../>, where object is an object element (typically a flow element) contained within a TextBlock, and value is a string that resolves to a Brush implementation value. 在代码中,GetForegroundSetForeground支持附加的属性用法。In code, the attached property usage is supported by GetForeground and SetForeground. 附加的属性用法并不常见,因为可以包含在 TextBlock 中的大多数元素都支持一个类似的非附加 Foreground 属性,TextBlock 用于呈现。The attached property usage is not common, because most elements that can be contained in a TextBlock support an analogous nonattached Foreground property, which the TextBlock uses for rendering.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ForegroundProperty
元数据属性设置为 trueMetadata properties set to true AffectsRender, SubPropertiesDoNotAffectRender, InheritsAffectsRender, SubPropertiesDoNotAffectRender, Inherits

适用于