XAML LinearGradientBrush: Rotating the Gradient

By Ken Getz

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:

<Rectangle Width="200" Height="100">
<LinearGradientBrush StartPoint="0,0.50"
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1.0" />

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
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 February 01, 2013 by Ken Getz

XAML Windows 8