如何在mainWindow中为linearGradientBrush调用我的方法?

时间:2014-07-10 14:49:33

标签: c# wpf xaml lineargradientbrush

我有一个问题。

我想在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>

是否可以这样做?

2 个答案:

答案 0 :(得分:0)

首先声明类型为ObservableCollection<Color>的属性,名为Colours

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

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

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

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

         

它并不完全是你想要的,但它与你想要的一样接近。

答案 1 :(得分:0)

您可以实施可以使用converterin the binding并将所有需要的参数传递为Binding.ConverterParameter

或者,因为这不需要&#34;绑定&#34;无论如何,你可以实现一个markup extension,它将参数作为构造函数参数:

<GradientStop Color="{me:GradientColor Green, Yellow, 2}" Offset="0"/>
相关问题