共用方式為


TextBlock.Foreground Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the Brush to apply to the text contents of the TextBlock.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Foreground As Brush
public Brush Foreground { get; set; }
<TextBlock>
  <TextBlock.Foreground>
    singleBrush
  </TextBlock.Foreground>
</TextBlock>
<TextBlock Foreground="colorString"/>

XAML Values

  • singleBrush
    A single object element for an object that derives from Brush. The object element is typically one of the following classes that are defined by Silverlight: LinearGradientBrush, RadialGradientBrush, ImageBrush, SolidColorBrush, VideoBrush.

  • colorString
    The Color for a SolidColorBrush expressed as a string value for the attribute. This can be a named color, an RGB value, or an ScRGB value. RGB or ScRGB values can also specify alpha information. For more information, see the "XAML Values" and "Predefined Colors" sections in Brush.

Property Value

Type: System.Windows.Media.Brush
The brush used to apply to the text contents. The default is a SolidColorBrush with a Color value of Black.

Remarks

Dependency property identifier field: ForegroundProperty

The Foreground property specifies a Brush for the rendered text. A Brush can represent a solid color, a linear or radial gradient, or an image.

Some brush types, such as SolidColorBrush, support a XAML attribute syntax. Other brush types, such as ImageBrush, LinearGradientBrush, and RadialGradientBrush, support only an object element syntax. This is why two versions of XAML syntax are shown for this property.

The default value of Black is the default at the property system level. When a TextBlock exists as a composite element of a control, it is quite likely that the value has been set by a default style, or is inheriting its value from the parent control or its default style. For details on how text elements can inherit property values, see Text and Fonts.

When you animate the Foreground, you must use indirect targeting. For instance, to animate the color of a SolidColorBrush that is the Foreground of a TextBlock, you could use the following:

<ColorAnimation ... Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" />.

Foreground settings for Run child elements override the settings on the containing TextBlock.

Examples

The following XAML examples shows how to set the Foreground property to a solid color and to a linear gradient.

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

<!-- TextBlock with a linear-gradient brush applied to the text. -->
<TextBlock
  Canvas.Top="100"
  FontFamily="Verdana"
  FontSize="32"
  FontWeight="Bold">
  LINEAR GRADIENT BRUSH
  <TextBlock.Foreground>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
      <GradientStop Color="Red" Offset="0.0" />
      <GradientStop Color="Orange" Offset="0.2" />
      <GradientStop Color="Yellow" Offset="0.4" />
      <GradientStop Color="Green" Offset="0.6" />
      <GradientStop Color="Blue" Offset="0.8" />
      <GradientStop Color="Violet" Offset="1.0" />
    </LinearGradientBrush>
  </TextBlock.Foreground>
</TextBlock>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.