SolidColorBrush SolidColorBrush SolidColorBrush SolidColorBrush Class

Definition

Paints an area with a solid color. The solid color is defined by a Color value.

public : sealed class SolidColorBrush : Brush, ISolidColorBrush
public sealed class SolidColorBrush : Brush, ISolidColorBrush
Public NotInheritable Class SolidColorBrush Inherits Brush Implements ISolidColorBrush
var solidColorBrush = new solidColorBrush();
<SolidColorBrush .../>
-or-
<SolidColorBrush>colorString</SolidColorBrush>
Inheritance
SolidColorBrushSolidColorBrushSolidColorBrushSolidColorBrush
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

The most common way to use SolidColorBrush is to define a XAML element as a resource in a ResourceDictionary, and then reference that resource later from other parts of UI definitions, styles or templates using either {StaticResource} markup extension or {ThemeResource} markup extension s.

<ResourceDictionary>
...
    <SolidColorBrush x:Key="BlockBackgroundBrush" Color="#FF557EB9"/>
...
</ResourceDictionary>
<Border Background="{StaticResource BlockBackgroundBrush}" 
    Width="80" Height="80"/>

There are several different ways to define a SolidColorBrush as an inline UI value rather than as a resource:

  • Select a predefined color by name, and rely on the XAML "shortcut" that this color will create a SolidColorBrush when it's parsed. For example, you can set the Fill of a Rectangle to "Red" like this:
<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>
<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>
  • Create a color within a 32-bit color palette by specifying the amounts of red, green, and blue to combine into a single solid color.
<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>
<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>
SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
auto greenBrush = ref new SolidColorBrush(Colors::Green);

Another way to define a new SolidColorBrush object is to use the FromArgb static utility method. This is useful if there is no named Colors value for the color you want.

SolidColorBrush myBrush = new SolidColorBrush(Color.FromArgb(255, 20, 20, 90));
auto myBrush = ref new SolidColorBrush(ColorHelper::FromArgb(255, 90, 200, 90));

Remarks

A SolidColorBrush is the most common type of Brush that is used for many possible UI properties that use a Brush to fill some or all of an object's visual area in app UI. Examples of some of the most commonly-used properties that use a Brush value include: Control.Background, Control.Foreground, Shape.Fill, Control.BorderBrush, Panel.Background, TextBlock.Foreground.

For these properties, a null value is often acceptable, and has the result that nothing is rendered there. If the element appears in the same coordinate space as other elements, the null value for a property such as Background causes that element to not register for purposes of hit-testing, and determining where an input event should be sourced from. Any pointer events, gestures or so on that occur on that point in the UI coordinate space are only detectable when there's a value other than null for the Brush property that influences rendering for that point.

A SolidColorBrush can be created that uses the Transparent value, and although this doesn't visually apply any changes to UI (it's transparent), that point is detectable for hit-testing purposes. So this is different than a Brush property with a null value. A Transparent brush can be useful for techniques such as creating overlay regions over UI elements where you want to intercept the hit testing with an element like a Rectangle, Border or panel. You might do this if the elements underneath aren't able to do their own hit-testing, but you still want to detect input events. For more info on hit testing, see "Hit testing" section of Mouse interactions.

Properties using brushes can be animated as part of transitions or decorative animations. You don't typically animate an entire SolidColorBrush object, you'd have to use a discrete Object animation and that's neither efficient nor aesthetic. Instead, you use property targeting to animate just the Color value, using one of the dedicated animation types that can animate a Color value. This usually involves having .(SolidColorBrush.Color) be a part of the Storyboard.TargetProperty value. For more info on property targeting and how to animate properties that use SolidColorBrush or other Brush values, see Storyboarded animations.

A SolidColorBrush is a shareable object, as are the other derived types of Brush such as LinearGradientBrush and ImageBrush. Because it's shareable a SolidColorBrush is sometimes defined in XAML as a resource in a XAML ResourceDictionary. The advantage of using shareable resources from XAML is that you're only creating the value once and applying it to multiple properties.

Applying a UIElement.Opacity value can change the color appearance of a SolidColorBrush property applied to an object. The UIElement.Opacity value can be cumulative depending on the layout of objects that overlap. The colors appear as expected only when the net Opacity value is 1. There's also a Brush.Opacity property that can affect the apparent color similarly. Brush.Opacity is usually left at its default value of 1, unless it's being deliberately animated for a fade-in or fade-out effect.

Brushes as XAML resources

Each of the Brush types that can be declared in XAML (SolidColorBrush, LinearGradientBrush, ImageBrush ) is intended to be defined as a resource, so that you can reuse that brush as a resource throughout your app. The XAML syntax shown for Brush types is appropriate for defining the brush as a resource. When you declare a brush as a resource, you also need an x:Key attribute that you'll later use to refer to that resource from other UI definitions. For more info on XAML resources and how to use x:Key attribute, see ResourceDictionary and XAML resource references.

The advantage of declaring brushes as resources is that it reduces the number of runtime objects that are needed to construct a UI: the brush is now shared as a common resource that's providing values for multiple parts of the object graph.

If you look at the existing control template definitions for Windows Runtime XAML controls, you'll see that the templates use brush resources extensively. Many of these resources are system resources, and they use the {ThemeResource} markup extension for the resource reference rather than {StaticResource} markup extension. For more info on how to use system resource brushes in your own control template XAML, see XAML theme resources.

Constructors

SolidColorBrush() SolidColorBrush() SolidColorBrush() SolidColorBrush()

Initializes a new instance of the SolidColorBrush class with no color.

public : SolidColorBrush()
public SolidColorBrush()
Public Sub New()
var solidColorBrush = new solidColorBrush();
See Also

SolidColorBrush(Color) SolidColorBrush(Color) SolidColorBrush(Color) SolidColorBrush(Color)

Initializes a new instance of the SolidColorBrush class with the specified Color.

public : SolidColorBrush(Color color)
public SolidColorBrush(Color color)
Public Sub New(color As Color)
var solidColorBrush = new solidColorBrush(color);
Parameters
color
Color Color Color Color

The color to apply to the brush.

Properties

Color Color Color Color

Gets or sets the color of this SolidColorBrush.

public : Color Color { get; set; }
public Color Color { get; set; }
Public ReadWrite Property Color As Color
var color = solidColorBrush.color;
solidColorBrush.color = color;
<SolidColorBrush Color="predefinedColorName"/>
- or -
<SolidColorBrush Color="#rgb"/>
- or -
<SolidColorBrush Color="#argb"/>
- or -
<SolidColorBrush Color="#rrggbb"/>
- or -
<SolidColorBrush Color="#aarrggbb"/>
- or -
<SolidColorBrush Color="sc#scR,scG,scB"/>
- or -
<SolidColorBrush Color="sc#scA,scR,scG,scB"/>
Value
Color Color Color Color

The brush's color. The default value is Transparent.

ColorProperty ColorProperty ColorProperty ColorProperty

Identifies the Color dependency property.

public : static DependencyProperty ColorProperty { get; }
public static DependencyProperty ColorProperty { get; }
Public Static ReadOnly Property ColorProperty As DependencyProperty
var dependencyProperty = Windows.UI.Xaml.Media.SolidColorBrush.colorProperty;
Value
DependencyProperty DependencyProperty DependencyProperty DependencyProperty

The identifier for the Color dependency property.

Dispatcher Dispatcher Dispatcher Dispatcher

Inherited from DependencyObject

Opacity Opacity Opacity Opacity

Inherited from Brush

OpacityProperty OpacityProperty OpacityProperty OpacityProperty

Inherited from Brush

RelativeTransform RelativeTransform RelativeTransform RelativeTransform

Inherited from Brush

RelativeTransformProperty RelativeTransformProperty RelativeTransformProperty RelativeTransformProperty

Inherited from Brush

Transform Transform Transform Transform

Inherited from Brush

TransformProperty TransformProperty TransformProperty TransformProperty

Inherited from Brush

Methods

ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty)

Inherited from DependencyObject

GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty)

Inherited from DependencyObject

GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty)

Inherited from DependencyObject

ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty)

Inherited from DependencyObject

RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback)

Inherited from DependencyObject

SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object)

Inherited from DependencyObject

UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64)

Inherited from DependencyObject

See Also