共用方式為


Background (Elements)

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

Gets or sets the Brush that describes the background of the object.

<object Background="colorString"  .../>
-or-
<object>
  <object.Background>
    singleBrush
  </object.Background>
</object>
value = object.Background
object.Background = 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 Color.

singleBrush

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

Property Value

Type: Brush

The brush that is used to fill the background of the object. If the object's Height or Width property value is equal to 0, the Background property value is ignored.

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

Managed Equivalent

Panel.Background

Remarks

The Background property of the Silverlight plug-in determines the background color of the client area underneath your Silverlight content, whereas this Background property determines the background color of an element.

The Background property specifies a Brush for the object. A Brush can represent a solid color, a linear or radial gradient, an image, or an applied video brush.

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 Background 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 Background, you generally use indirect targeting. For instance, if you are animating the color of a SolidColorBrush that is the Background of a Canvas, the syntax is <ColorAnimation ... Storyboard.TargetProperty="(Canvas.Background).(SolidColorBrush.Color)" />. You could also take the less common approach of using explicit property element / object element syntax (for example, declaring SolidColorBrush as an object element) until you reach specific properties that take a Color value.

InkPresenter is a derived class of Canvas and inherits its Background property. The Background for an InkPresenter is applied behind any strokes that are presented on the surface.

For purposes of rendering, the null default value for Background evaluates as a transparent brush. However, a brush with the explicit color of Transparent will cause that region to be hit-tested.

Example

The following XAML example shows how to define a LinearGradientBrush object that is used as the Background property value for the Canvas object.

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="80"
  >

  <!-- Define a LinearGradientBrush for the Canvas Background property. -->
  <Canvas.Background>
    <LinearGradientBrush>
      <GradientStop Color="Yellow" Offset="0.0" />
      <GradientStop Color="Orange" Offset="0.5" />
      <GradientStop Color="Red" Offset="1.0" />
    </LinearGradientBrush>
  </Canvas.Background>
</Canvas>

The following illustration shows the rendered output of the previous example, which uses a LinearGradientBrush object as the Background.

Canvas object rendered with a LinearGradientBrush

Rectangle with gradient.

Applies To

Canvas

Grid (Silverlight 2)

InkPresenter

PasswordBox (Silverlight 2)

StackPanel (Silverlight 2)

TextBox (Silverlight 2)