Using Element.Property Syntax for the SolidColorBrush Class

By Ken Getz | January 30, 2013

Element.Property syntax to alter opacity

Even once you've learned about shapes and geometries, and creating your own drawings, youneed to learn brushes in order to create the kind of content you'll need in rich, XAML-based applications.

Think of a brush as a means to paint, or fill an area with its output. Differentbrushes provide different types of output: Some paint with a solid color, others paint with a gradient, pattern, image, or drawing. This article focuses on specifying colors for the simplest brush, the SolidColorBrush class, using Property.Element syntax to describe the SolidColorBrush. This syntax is more verbose, but it allows you to specify additional settings,including the brush's opacity.

Using Element.Property syntax when specifying a color, you can get the best of both worlds: That is, you can specify a color using its name as a string, and you can also set the Opacity property separately. If you want to alter the opacity of a color at runtime, this is the best solution(because the opacity can be set as a separate property, rather than requiring you to alter the color itself). Using this syntax, you might see the Fill property for a Rectangle shape set like the following:

<Rectangle Width="75" Height="75">
<SolidColorBrush Color="Red" />

The Figure below demonstrates this syntax. In the following example, the Opacity property of a SolidColorBrush has been bound to the Value property of the Slider, and the SolidColorBrush object provides the Fill property for a Rectangle:

<Rectangle Canvas.Left="60"
Width="203" Height="70"
Stroke="Blue" StrokeThickness="5">
 Opacity="{Binding Value,
 ElementName=opacitySlider}" />

Thumbnail for 559This post is an excerpt from the online courseware for our Windows 8 Using XAML: Bindings, Shapes, and Animation course written by expert Ken Getz.

Ken Getz

Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.

This blog entry was originally posted January 30, 2013 by Ken Getz