wpf之漸變色LinearGradientBrush


xmal代碼:

 1 <Grid Name="grid1">
 2      <Grid.Background>
 3          <LinearGradientBrush>
 4              <LinearGradientBrush.GradientStops>
 5                  <GradientStop Offset="0" Color="red"></GradientStop>
 6                  <GradientStop Offset="0.5" Color="blue"></GradientStop>
 7                  <GradientStop Offset="1" Color="Yellow"></GradientStop>
 8              </LinearGradientBrush.GradientStops>
 9          </LinearGradientBrush>
10      </Grid.Background>
11 </Grid>

也可以用C#代碼實現,等價的C#代碼為:

 1 public MainWindow()
 2 {
 3      InitializeComponent();
 4      MyLinearGradientBrush();
 5 }
 6 
 7 public void MyLinearGradientBrush()
 8 {
 9      LinearGradientBrush brush = new LinearGradientBrush();
10 
11      GradientStop gs1 = new GradientStop();
12      gs1.Offset = 0;
13      gs1.Color = Colors.Red;
14      brush.GradientStops.Add(gs1);
15 
16      GradientStop gs2 = new GradientStop();
17      gs2.Offset = 0.5;
18      gs2.Color = Colors.Blue;
19      brush.GradientStops.Add(gs2);
20 
21      GradientStop gs3 = new GradientStop();
22      gs3.Offset = 1;
23      gs3.Color = Colors.Yellow;
     //因為GradientStops屬性返回一個GradientStopCollection對象,而且GradientStopCollection類實現了IList接口
24 brush.GradientStops.Add(gs3);//所以這句代碼的本質是IList list = brush.GradientStops; list.Add(gs3); 25 26 grid1.Background = brush; 27 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM