0

I have a problem.

I would like to call my method in xaml (mainWindow) in LinearGradientBrush --> GradientStop

So I would like to change color in background for an animation. I have a function which has several parameters as :

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;
}

In code XAML :

<LinearGradientBrush StartPoint="0.0, 0.6" EndPoint="1.0, 0.6">
    <GradientStop Color="{ Binding GetGradientColors(green, yellow, 2)}" Offset="0"/>
</LinearGradientBrush>

Is it possible to do this?

4

2 回答 2

0

首先声明一个类型的属性ObservableCollection<Color>,比如说命名Colours

public ObservableCollection<Color> Colours { get; set; }

然后在构造函数中设置属性:

Colours = GetGradientColors(Colors.Green, Colors.Yellow, 2);

然后数据在 XAML 中绑定到它:

这并不完全是您想要的,但它与您将获得的一样接近。

于 2014-07-10T15:12:44.970 回答
0

您可以实现一个可以在绑定中使用的转换器,并将所有需要的参数作为.Binding.ConverterParameter

或者,由于这无论如何都不需要“绑定”,因此您可以实现一个标记扩展,它将参数作为构造函数参数:

<GradientStop Color="{me:GradientColor Green, Yellow, 2}" Offset="0"/>
于 2014-07-10T15:21:41.790 回答