SolidColorBrush Class
TOC
Collapse the table of content
Expand the table of content

SolidColorBrush Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Paints an area with a solid color.

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

[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 methodSolidColorBrush()Initializes a new instance of the SolidColorBrush class with no color.
Public methodSolidColorBrush(Color)Initializes a new instance of the SolidColorBrush class with the specified Color.
Top

  NameDescription
Public propertyColorGets or sets the color of this SolidColorBrush.
Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyOpacityGets or sets the degree of opacity of a Brush. (Inherited from Brush.)
Public propertyRelativeTransformGets or sets the transformation that is applied to the brush using relative coordinates. (Inherited from Brush.)
Public propertyTransformGets or sets the transformation that is applied to the brush. (Inherited from Brush.)
Top

  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAnimationBaseValueReturns any base value established for a Windows Phone dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberColorPropertyIdentifies 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, Windows Phone 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.

    
    <!-- This rectangle's fill is painted with a red SolidColorBrush,
    described using a named color. -->
    <Rectangle Width="100" Height="100" Margin="20" Fill="Red" />
    
    
    
  • 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.

    
    <!-- This rectangle's background is painted with a red SolidColorBrush,
    described using hexadecimal notation. -->
    <Rectangle Width="100" Height="100" Margin="20" Fill="#FFFF0000" />
    
    
    
  • 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 Fillproperties 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.

    
    <!-- Both of these rectangles' fills are painted with red
    SolidColorBrush objects, described using object element
    syntax. -->
    <Rectangle Width="100" Height="100" Margin="20">
        <Rectangle.Fill>
            <SolidColorBrush Color="Red" />
        </Rectangle.Fill>
    </Rectangle>
    
    <Rectangle Width="100" Height="100"  Margin="20">
        <Rectangle.Fill>
            <SolidColorBrush Color="#FFFF0000" />
        </Rectangle.Fill>
    </Rectangle>
    
    
    

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2017 Microsoft