在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>
效果圖: