我想通了。以下是C#中的代碼:
Rectangle rect = new Rectangle();
GradientStop gs_black = new GradientStop();
GradientStop gs_white = new GradientStop();
LinearGradientBrush lgb = new LinearGradientBrush();
private void cb_test_Click(object sender, RoutedEventArgs e)
{
rect.Width = 1280;
rect.Height = 1024;
gs_black.Offset = 0;
gs_black.Color = Color.FromArgb(255, 0, 0, 0);
gs_white.Offset = .25;
gs_white.Color = Color.FromArgb(255, 255, 255, 255);
lgb.StartPoint = new Point(0, 0);
lgb.EndPoint = new Point(0, 1);
lgb.GradientStops = new GradientStopCollection();
lgb.GradientStops.Add(gs_black);
lgb.GradientStops.Add(gs_white);
rect.Fill = lgb;
canvasname.Children.Add(rect);
}
private void cb_change_color_Click(object sender, RoutedEventArgs e)
{
lgb.GradientStops.Remove(gs_black);
gs_black.Offset = 0;
gs_black.Color = Color.FromArgb(255, 0, 255, 0);
lgb.GradientStops.Add(gs_black);
}