Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Silverlight 3
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
SolidColorBrush Class

Paints an area with a solid color.

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<ContentPropertyAttribute("Color", True)> _
Public NotInheritable Class SolidColorBrush _
    Inherits Brush
Visual Basic (Usage)
Dim instance As SolidColorBrush
C#
[ContentPropertyAttribute("Color", true)]
public sealed class SolidColorBrush : Brush
XAML Object Element Usage
<SolidColorBrush .../>
-or-
<SolidColorBrush>colorString</SolidColorBrush>
XAML Attribute Usage
<object property="predefinedColor"/>
- or -
<object property="#rgb"/>
- or -
<object property="#argb"/>
- or -
<object property="#rrggbb"/>
- or -
<object property="#aarrggbb"/>
- or -
<object property="sc#scR,scG,scB"/>
- or -
<object property="sc# scA,scR,scG,scB"/>

XAML Values

colorString

Any one of the color string conventions in the remainder of this table.

predefinedColor

One of the colors predefined by the Colors class (static properties), or one of the other named colors. See Remarks.

rgb

A three-character hexadecimal value. The first character specifies the color's R value, the second character specifies the G value, and the third character specifies the B value. For example, #00F.

argb

A four-character hexadecimal value. The first character specifies the color's A value, the second character specifies its R value, the third character specifies the G value, and the fourth character specifies its B value. For example, #F00F.

rrggbb

A six-character hexadecimal value. The first two characters specify the color's R value, the next two specify its G value, and the final two specify its B value. For example, #0000FF.

aarrggbb

An eight-character hexadecimal value. The first two characters specify the color's A value, the next two specify its R value, the next two specify its G value, and the final two specify its B value. For example, #FF0000FF.

scA

The color's ScA value as a value between 0 and 1. ScA is not exposed as a Color property directly.

scR

The color's ScR value as a value between 0 and 1. ScR is not exposed as a Color property directly

scG

The color's ScG value as a value between 0 and 1. ScG is not exposed as a Color property directly

scB

The color's ScB value as a value between 0 and 1. ScB is not exposed as a Color property directly

The SolidColorBrush is the most basic brush that applies an appearance to an object. A SolidColorBrush can be specified in XAML as an attribute value, through a type conversion syntax that uses several conventions for the meaning of the string for specifying a color. Other brushes (for example LinearGradientBrush) require a property element syntax. Object element syntax for a SolidColorBrush is possible, and is useful for the following reasons:

  • You want to provide a Name to the object element and target its properties later.

  • You are defining a SolidColorBrush as a resource.

  • You are setting an Opacity on the SolidColorBrush.

You can animate a SolidColorBrush using either ColorAnimation or ColorAnimationUsingKeyFrames. Usually this is done not by animating the Color property of a SolidColorBrush, but is instead by using indirect targeting of a property such as Shape..::.Fill that takes a Brush. The ColorAnimation reference topic shows an example.

A SolidColorBrush is created as part of a type conversion syntax enabled by the Brush class and by SolidColorBrush itself. The syntax is described above in the "XAML Attribute Usage" subsection of the Syntax section. This syntax enables you to specify a string value for an attribute that takes a Brush, and the string is interpreted within a number of possible conventions, which include named colors, RGB, or ScRGB. Both RGB and ScRBG can specify an alpha value. For more information on the XAML syntax, see Color and Brush.

Predefined Colors

ms635533.JOLT_local_-485375713_art_color_table(en-us,VS.95).png

Setting Predefined Colors for a SolidColorBrush in Code

Setting predefined colors for the 16 color names that are defined as Colors static values just uses the static values as the setting value, which can be done either with the constructor overload or by setting Color as a property after construction.

//constructor technique
SolidColorBrush scb = new SolidColorBrush(Colors.Black);
//postconstruction technique
SolidColorBrush scb2 = new SolidColorBrush();
scb2.Color = Colors.Black;

One of the most common operations in any presentation technology is to paint an area with a solid color. To accomplish this task, Silverlight provides the SolidColorBrush class. The following sections 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 of a Rectangle to Red or MediumBlue. The example uses the name of a predefined SolidColorBrush to set the Fill of a Rectangle.

    Run this sample

    XAML
    <StackPanel>
    
      <!-- This rectangle's fill is painted with a red SolidColorBrush,
           described using a named color. -->
      <Rectangle Width="100" Height="100" Fill="Red" />
    </StackPanel>
  • 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, where rr is a two character hexadecimal number specifying the relative amount of red, gg specifies the amount of green, and bb specifies the amount of blue. Additionally, the color can be specified as #aarrggbb, where aa specifies the alpha value, or transparency, of the color. This approach enables you to create colors that are partially transparent. In the following example, the Fill of a Rectangle is set to fully opaque red using hexadecimal notation.

    Run this sample

    XAML
    <StackPanel>
    
      <!-- This rectangle's background is painted with a red SolidColorBrush,
           described using hexadecimal notation. -->
      <Rectangle Width="100" Height="100" Fill="#FFFF0000" />
    </StackPanel>
  • Use property element syntax to describe a SolidColorBrush. This syntax is more verbose but enables you to specify additional settings, such as the brush's opacity. In the following example, the Fill properties of two Rectangle elements are set to fully opaque red. The first brush's color is described using a predefined color name. The second brush's color is described using hexadecimal notation.

    Run this sample

    XAML
    <StackPanel>
     
      <!-- Both of these rectangles' fills are painted with red
           SolidColorBrush objects, described 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> 
    </StackPanel>
System..::.Object
  System.Windows..::.DependencyObject
    System.Windows.Media..::.Brush
      System.Windows.Media..::.SolidColorBrush
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker