Поделиться через


SolidColorBrush Class

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

Paints an area with a solid color.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Media.Brush
      System.Windows.Media.SolidColorBrush

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

Syntax

'Declaration
<ContentPropertyAttribute("Color", True)> _
Public NotInheritable Class SolidColorBrush _
    Inherits Brush
[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.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone SolidColorBrush() Initializes a new instance of the SolidColorBrush class with no color.
Public methodSupported by Silverlight for Windows Phone SolidColorBrush(Color) Initializes a new instance of the SolidColorBrush class with the specified Color.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone Color Gets or sets the color of this SolidColorBrush.
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows Phone Opacity Gets or sets the degree of opacity of a Brush. (Inherited from Brush.)
Public propertySupported by Silverlight for Windows Phone RelativeTransform Gets or sets the transformation that is applied to the brush using relative coordinates. (Inherited from Brush.)
Public propertySupported by Silverlight for Windows Phone Transform Gets or sets the transformation that is applied to the brush. (Inherited from Brush.)

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows 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 Phone GetAnimationBaseValue Returns 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 Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows Phone ColorProperty Identifies the Color dependency property.

Top

Remarks

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;

Examples

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="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 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="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 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 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.

    Run this sample

    <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 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>
    

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

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

Platforms

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

Thread Safety

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