SolidColorBrush

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

Paints an area with a solid color.

<object property="colorString"  .../>
-or-
<SolidColorBrush   .../>
object.property = "colorString"

colorString Grammar

For information about colorString grammar, see Color.

Managed Equivalent

SolidColorBrush

Remarks

The SolidColorBrush object is perhaps the most basic brush that applies an appearance to an object. You can specify a SolidColorBrush in XAML as an attribute value, through a type conversion syntax that uses several conventions for the meaning of the string. Other brushes (for example LinearGradientBrush) require a property element syntax. It is possible for you to use property element syntax for a Brush property along with object element syntax <SolidColorBrush.../>; this might be useful if you want to provide a Name value for the object element and target its properties later.

You can animate a SolidColorBrush by using either the ColorAnimation or ColorAnimationUsingKeyFrames objects. To animate a SolidColorBrush, you would typically use indirect targeting of a property such as Fill that takes a Brush, rather than animating the Color property of a SolidColorBrush. For an example, see ColorAnimation.

For more information on basic concepts, see Brushes. Note that the Brushes topic is written primarily for users of the managed API, and may not have code examples or specific information that address the JavaScript API scenarios.

Example

One of the most common operations in any platform is to paint an area with a solid color. To accomplish this task, Silverlight provides the SolidColorBrush object. The following examples describe the different ways to paint with a SolidColorBrush.

To paint an area with a solid color in XAML, use one of the following options:

  • Select a predefined SolidColorBrush by name. For example, you can set the Fill value of a Rectangle object to Red or MediumBlue. The following example uses the name of a predefined SolidColorBrush to set the Fill of a Rectangle.

    <Canvas
      xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- This rectangle's fill is painted with a red SolidColorBrush,
           described by using a named color. -->
      <Rectangle Width="100" Height="100" Fill="Red" />
    </Canvas>
    
  • Choose a color from the 32-bit color palette by specifying the amounts of red, green, and blue to combine into a single solid color. The format for specifying a color from the 32-bit palette is #rrggbb (rr, gg, and bb are two-character hexadecimal numbers that specify the relative amount of red, green, and blue, respectively). Additionally, you can specify the color as #aarrggbb (aa specifies the alpha value, or transparency, of the color). This approach enables you to create colors that are partially transparent. The following example sets the Fill of a Rectangle to fully opaque red by using hexadecimal notation.

    <Canvas 
      xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- This rectangle's background is painted with a red SolidColorBrush,
           described by using hexadecimal notation. -->
      <Rectangle Width="100" Height="100" Fill="#FFFF0000" />
    </Canvas>
    
  • Use property element syntax to describe a SolidColorBrush. This syntax is more verbose, but it enables you to specify additional settings, such as the brush's opacity. The following example sets the Fill properties of two Rectangle elements to fully opaque red. The first brush's color is described by using a predefined color name. The second brush's color is described by using hexadecimal notation.

    <Canvas 
      xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- Both of these rectangles' fills are painted with red
           SolidColorBrush objects, described by using object element
           syntax. -->
      <Rectangle Width="100" Height="100">
        <Rectangle.Fill>
          <SolidColorBrush Color="Red" />
        </Rectangle.Fill>
      </Rectangle>
    
      <Rectangle Width="100" Height="100" Canvas.Top="110">
        <Rectangle.Fill>
          <SolidColorBrush Color="#FFFF0000" />
        </Rectangle.Fill>
      </Rectangle> 
    </Canvas>
    

See Also

Reference