Partager via


Foreground

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

Gets or sets the Brush to apply to the content in this object.

<object Foreground="colorString"  .../>
-or-
<object>
  <object.Foreground>
    singleBrush
  </object.Foreground>
</object>
value = object.Foreground
object.Foreground = value

XAML Values

Value

Description

colorString

The Color for a SolidColorBrush expressed as an attribute string. This can be a named color, an RGB value, or an ScRGB value. RGB or ScRGB may also specify alpha information. See the "colorString Grammar" section in the Color reference topic.

singleBrush

Within opening and closing property elements for object.Foreground, exactly one object element for an object that derives from Brush. The object element can be one of the following: LinearGradientBrush, RadialGradientBrush, ImageBrush, SolidColorBrush, VideoBrush.

Property Value

Type: Brush

The brush used to apply to the text contents.

This property is read/write. The default value is Black.

Remarks

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.

If you are setting this property in script, the most common technique is to use the colorString grammar to specify the value as a string. Alternatively, you might set the value to an object such as LinearGradientBrush by specifying the XAML that defines the brush as the input for CreateFromXaml, and setting Foreground to the CreateFromXAML output.

Some brush types (SolidColorBrush) support a XAML attribute syntax, whereas other brush types (ImageBrush, LinearGradientBrush, RadialGradientBrush) support only an object element syntax. This is why two versions of XAML syntax are shown for this property.

When animating a Foreground, you must use indirect targeting. For example, if you are animating the color of a SolidColorBrush that is the Foreground of a TextBlock, the syntax would be <ColorAnimation ... Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" />.

The "Applies To" section of this topic lists LineBreak, because it does exist in the object model and you can get and set it. However, all LineBreak properties are ignored for rendering purposes.

Example

The following XAML example shows how to set the Foreground property to a solid color and a linear gradient. Notice that the Foreground property can also be used in a Run in order to display a different color from the Foreground property value of the TextBlock.

<!-- TextBlock with a single brush applied to the text. -->
<TextBlock
  FontSize="32"
  FontWeight="Bold"
  Foreground="Maroon">
  Maroon
</TextBlock>

<!-- TextBlock with three brushes applied to the text. -->
<TextBlock
  Canvas.Top="60"
  FontFamily="Arial"
  FontSize="32"
  FontWeight="Bold"
  Foreground="Navy">
  Navy
  <Run Text="DodgerBlue " Foreground="DodgerBlue"/>
  <Run Text="LightSteelBlue " Foreground="LightSteelBlue"/>
</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.RenderTransform>
    <ScaleTransform ScaleY="3.0" />
  </TextBlock.RenderTransform>
</TextBlock>

Applies To

LineBreak

Run

PasswordBox (Silverlight 2)

TextBlock

TextBox (Silverlight 2)