【发布时间】:2014-07-10 14:49:32
【问题描述】:
我有问题。
我想在 LinearGradientBrush 的 xaml (mainWindow) 中调用我的方法 --> GradientStop
所以我想改变动画的背景颜色。我有一个函数,它有几个参数:
public static List<Color> GetGradientColors(Color start, Color end, int steps)
{
return GetGradientColors(start, end, steps, 0, steps - 1);
}
public static List<Color> GetGradientColors(Color start, Color end, int steps, int firstStep, int lastStep)
{
var colorList = new List<Color>();
if (steps <= 0 || firstStep < 0 || lastStep > steps - 1)
return colorList;
double aStep = (end.A - start.A) / steps;
double rStep = (end.R - start.R) / steps;
double gStep = (end.G - start.G) / steps;
double bStep = (end.B - start.B) / steps;
for (int i = firstStep; i < lastStep; i++)
{
byte a = (byte)(start.A + (int)(aStep * i));
byte r = (byte)(start.R + (int)(rStep * i));
byte g = (byte)(start.G + (int)(gStep * i));
byte b = (byte)(start.B + (int)(bStep * i));
colorList.Add(Color.FromArgb(a, r, g, b));
}
return colorList;
}
在代码 XAML 中:
<LinearGradientBrush StartPoint="0.0, 0.6" EndPoint="1.0, 0.6">
<GradientStop Color="{ Binding GetGradientColors(green, yellow, 2)}" Offset="0"/>
</LinearGradientBrush>
可以这样做吗?
【问题讨论】:
-
你为什么要这么做?渐变画笔会自行插值,您只需为
Green停一停,为Yellow停一停。
标签: c# wpf xaml lineargradientbrush