TextBlock.Foreground Property
Namespace: System.Windows.Controls
Assembly: System.Windows (in System.Windows.dll)
<TextBlock> <TextBlock.Foreground> singleBrush </TextBlock.Foreground> </TextBlock>
<TextBlock Foreground="colorString"/>
XAML Values
Property Value
Type: System.Windows.Media.BrushThe brush used to apply to the text contents. The default is a SolidColorBrush with a Color value of Black.
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.
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>
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.