wpf 分别用 xaml 和后台代码实现 色彩渐变

xaml 方法:

  1. <Window x:Class=”WpfApplication1.MainWindow”
  2.         xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
  3.         xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
  4.         Title=”MainWindow” Height=”350″ Width=”525″>
  5.     <Grid x:Name=”grid1″>
  6.         <!–xaml中设置渐变效果–>
  7.         <!–<Grid.Background>
  8.             <LinearGradientBrush>
  9.                 <GradientStopCollection>
  10.                     <GradientStop Offset=”0″ Color=”Black” />
  11.                     <GradientStop Offset=”0.6″ Color=”white”  />
  12.                 </GradientStopCollection>
  13.             </LinearGradientBrush>
  14.         </Grid.Background>–>
  15.     </Grid>
  16. </Window>

 

后台方法:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace WpfApplication1
  15. {
  16.     /// <summary>
  17.     /// MainWindow.xaml 的交互逻辑
  18.     /// </summary>
  19.     public partial class MainWindow : Window
  20.     {
  21.         public MainWindow()
  22.         {
  23.             InitializeComponent();
  24.             LinearGradientBrush brush = new LinearGradientBrush();
  25.             brush.GradientStops.Add(new GradientStop(color:Colors.Black,offset:0));
  26.             brush.GradientStops.Add(new GradientStop(color: Colors.White, offset: 0.6));
  27.             grid1.Background = brush;
  28.         }
  29.     }
  30. }

标签