SolidColorBrush
Paints an area with a solid color.
XAML | |
Scripting |
object.property = "colorString"
-or-
See CreateFromXAML.
|
colorString Grammar
For details on the colorString grammar, see Color.
Properties
Color, Name, Opacity, RelativeTransform, Transform
Methods
Equals, FindName, GetHost, GetValue, SetValue
Remarks
The SolidColorBrush is perhaps 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. Other brushes (for example LinearGradientBrush) require a property element syntax. Property element syntax for a Brush property along with object element syntax <SolidColorBrush.../> is possible, and might be useful if you want to provide a Name to the object element and target its properties later.
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 Fill that takes a Brush. The ColorAnimation reference topic shows an example.
Examples
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 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.
XAML <Canvas
xmlns="https://schemas.microsoft.com/client/2007" 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 digit 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.
XAML <Canvas
xmlns="https://schemas.microsoft.com/client/2007" 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 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.
XAML <Canvas
xmlns="https://schemas.microsoft.com/client/2007" 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>
See Also