GradientStop Class
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable)] public sealed class GradientStop : Animatable, IFormattable
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable) */ public final class GradientStop extends Animatable implements IFormattable
LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable) public final class GradientStop extends Animatable implements IFormattable
<GradientStop .../>
Use this class to describe the colors in a LinearGradientBrush or RadialGradientBrush.
Note that this class does not provide an opacity property; to make a GradientStop semi-transparent, set its GradientStop.Color property with a transparent Color.
Freezable Features
A GradientStop is a type of Freezable object and therefore can be frozen to improve performance. For information about Freezable features, such as freezing and cloning, see the Freezable Objects Overview.
This example shows how to use the LinearGradientBrush class to paint an area with a linear gradient. In the following example, the Fill of a Rectangle is painted with a diagonal linear gradient that transitions from yellow to red to blue to lime green.
<!-- This rectangle is painted with a diagonal linear gradient. --> <Rectangle Width="200" Height="100"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="0.25" /> <GradientStop Color="Blue" Offset="0.75" /> <GradientStop Color="LimeGreen" Offset="1.0" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>
Rectangle diagonalFillRectangle = new Rectangle(); diagonalFillRectangle.Width = 200; diagonalFillRectangle.Height = 100; // Create a diagonal linear gradient with four stops. LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(); myLinearGradientBrush.StartPoint = new Point(0,0); myLinearGradientBrush.EndPoint = new Point(1,1); myLinearGradientBrush.GradientStops.Add( new GradientStop(Colors.Yellow, 0.0)); myLinearGradientBrush.GradientStops.Add( new GradientStop(Colors.Red, 0.25)); myLinearGradientBrush.GradientStops.Add( new GradientStop(Colors.Blue, 0.75)); myLinearGradientBrush.GradientStops.Add( new GradientStop(Colors.LimeGreen, 1.0)); // Use the brush to paint the rectangle. diagonalFillRectangle.Fill = myLinearGradientBrush;
The following illustration shows the gradient created by the previous example.
To create a horizontal linear gradient, change the StartPoint and EndPoint of the LinearGradientBrush to (0,0.5) and (1,0.5). In the following example, a Rectangle is painted with a horizontal linear gradient.
<!-- This rectangle is painted with a horizontal linear gradient. --> <Rectangle Width="200" Height="100"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="0.25" /> <GradientStop Color="Blue" Offset="0.75" /> <GradientStop Color="LimeGreen" Offset="1.0" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>
Rectangle horizontalFillRectangle = new Rectangle(); horizontalFillRectangle.Width = 200; horizontalFillRectangle.Height = 100; // Create a horizontal linear gradient with four stops. LinearGradientBrush myHorizontalGradient = new LinearGradientBrush(); myHorizontalGradient.StartPoint = new Point(0,0.5); myHorizontalGradient.EndPoint = new Point(1,0.5); myHorizontalGradient.GradientStops.Add( new GradientStop(Colors.Yellow, 0.0)); myHorizontalGradient.GradientStops.Add( new GradientStop(Colors.Red, 0.25)); myHorizontalGradient.GradientStops.Add( new GradientStop(Colors.Blue, 0.75)); myHorizontalGradient.GradientStops.Add( new GradientStop(Colors.LimeGreen, 1.0)); // Use the brush to paint the rectangle. horizontalFillRectangle.Fill = myHorizontalGradient;
The following illustration shows the gradient created by the previous example.
To create a vertical linear gradient, change the StartPoint and EndPoint of the LinearGradientBrush to (0.5,0) and (0.5,1). In the following example, a Rectangle is painted with a vertical linear gradient.
<!-- This rectangle is painted with a vertical gradient. --> <Rectangle Width="200" Height="100"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="0.25" /> <GradientStop Color="Blue" Offset="0.75" /> <GradientStop Color="LimeGreen" Offset="1.0" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>
Rectangle verticalFillRectangle = new Rectangle(); verticalFillRectangle.Width = 200; verticalFillRectangle.Height = 100; // Create a vertical linear gradient with four stops. LinearGradientBrush myVerticalGradient = new LinearGradientBrush(); myVerticalGradient.StartPoint = new Point(0.5,0); myVerticalGradient.EndPoint = new Point(0.5,1); myVerticalGradient.GradientStops.Add( new GradientStop(Colors.Yellow, 0.0)); myVerticalGradient.GradientStops.Add( new GradientStop(Colors.Red, 0.25)); myVerticalGradient.GradientStops.Add( new GradientStop(Colors.Blue, 0.75)); myVerticalGradient.GradientStops.Add( new GradientStop(Colors.LimeGreen, 1.0)); // Use the brush to paint the rectangle. verticalFillRectangle.Fill = myVerticalGradient;
The following illustration shows the gradient created by the previous example.
For additional examples, see the Brushes Sample. For more information about gradients and other types of brushes, see Painting with WPF Brushes.
More Code
| How to: Paint an Area with a Radial Gradient | This example shows how to use the RadialGradientBrush class to paint an area with a radial gradient. |
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.GradientStop
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.