Tag Archives: xaml brushes

XAML LinearGradientBrush: Rotating the Gradient

rotate the gradient

In a previous article we started discussing brushes, specifically the SolidColorBrush. This article switches focus to the LinearGradientBrush and rotating gradients.

A LinearGradientBrush allows you to paint an area with a linear gradient. When creating a linear gradient, you control the direction and position of the gradient, along with the colors that make up the gradientand the locations at which the colors change.

Rotating the Gradient

You can easily rotate a gradient by changing its StartPoint and EndPoint values. In the example shown in the Figure below, the start point and end point havebeen changed so that they create a horizontal line.

The following markup changes only the StartPoint and EndPoint values:

The endpoints don’t need to be horizontal, vertical, or diagonal, of course. The Figure below shows a gradient from the sample (second column, second row) that uses a slightly different angle, and shows its effect on the gradient.

gradient line

TIP: If you would prefer not to have the coordinate mapping system relative to the bounding box, you can modify the MappingMode property of the LinearGradientBrush to Absolute, as opposed to its default value of RelativeToBoundingBox.

ldn-expertkgetzThis 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 is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Using Element.Property Syntax for the SolidColorBrush Class

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:

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:

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 is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.