在MSDN文档库里可以查到,Rectangle.Fill的类型是Brush。Brush是一个抽象类,凡是以Brush为基类的类都可作为Fill属性的值。Brush的派生类有很多:
* SolidColorBrush:单色画刷
* LinearGradientBrush:线性渐变画刷
* RadialGradientBrush:径向渐变画刷
* ImageBrush:位图画图
* DrawingBrush:矢量图画刷
* VisualBrush:可视元素画刷
默认的线性渐变是沿对角方向进行的。默认情况下,线性渐变的 StartPoint 是被绘制区域的左上角值为(0,0) 的 Point,其 EndPoint 是被绘制区域的右下角值为(1,1) 的 Point。所得渐变的颜色是沿着对角方向路径插入的。
示例:
1 <Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="200" Width="300"> 9 <Grid > 10 <Rectangle Width="200" Height="100"> 11 <Rectangle.Fill> 12 <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" Opacity="0.8"> 13 <GradientStop Offset="0.0" Color="Yellow" /> 14 <GradientStop Offset="0.25" Color="Red" /> 15 <GradientStop Offset="0.75" Color="Blue" /> 16 <GradientStop Offset="1.0" Color="LimeGreen" /> 17 </LinearGradientBrush> 18 </Rectangle.Fill> 19 </Rectangle> 20 </Grid> 21 </Window>
效果图:
垂直渐变的渐变轴
1 <Window x:Class="WpfApp3.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp3" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="200" Width="300"> 9 <Grid> 10 <Rectangle Width="200" Height="100"> 11 <Rectangle.Fill> 12 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" Opacity="0.8"> 13 <GradientStop Offset="0.0" Color="Yellow" /> 14 <GradientStop Offset="0.25" Color="Red" /> 15 <GradientStop Offset="0.75" Color="Blue" /> 16 <GradientStop Offset="1.0" Color="LimeGreen" /> 17 </LinearGradientBrush> 18 </Rectangle.Fill> 19 </Rectangle> 20 </Grid> 21 </Window>
效果图: