Export (0) Print
Expand All

SolidColorBrush Class

Paints an area with a solid color.

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)

[ContentPropertyAttribute("Color", true)]
public sealed class SolidColorBrush : Brush
<SolidColorBrush .../>
-or-
<SolidColorBrush>colorString</SolidColorBrush>
<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 type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSolidColorBrush()Initializes a new instance of the SolidColorBrush class with no color.
Public methodSupported by Silverlight for Windows PhoneSolidColorBrush(Color)Initializes a new instance of the SolidColorBrush class with the specified Color.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneColorGets or sets the color of this SolidColorBrush.
Public propertySupported by Silverlight for Windows PhoneDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows PhoneOpacityGets or sets the degree of opacity of a Brush. (Inherited from Brush.)
Public propertySupported by Silverlight for Windows PhoneRelativeTransformGets or sets the transformation that is applied to the brush using relative coordinates. (Inherited from Brush.)
Public propertySupported by Silverlight for Windows PhoneTransformGets or sets the transformation that is applied to the brush. (Inherited from Brush.)
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetAnimationBaseValueReturns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberSupported by Silverlight for Windows PhoneColorPropertyIdentifies the Color dependency property.
Top

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 (unless you use a reference, such as {StaticResource}). 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

Colors.

Setting Predefined Colors for a SolidColorBrush in Code

Setting predefined colors for the 16 color names that are defined as Colors static values 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

    
    <Canvas
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- This rectangle's fill is painted with a red SolidColorBrush,
           described 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, 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

    
    <Canvas 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- This rectangle's background is painted with a red SolidColorBrush,
           described 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 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

    
    <Canvas 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
      <!-- 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> 
    </Canvas>
    
    
    

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft