LinearGradientBrush,两端都有固定宽度的渐变

时间:2013-02-19 16:13:06

标签: wpf lineargradientbrush

我想使用LinearGradientBrush用渐变填充画布上的矩形范围。问题是,我希望渐变在矩形的两边看起来都是一样的,无论矩形有多大。

我想要绘制的是这样的:

http://imgur.com/B5efn7c

这是我的第一次尝试:

LinearGradientBrush brush = new LinearGradientBrush();
brush.SpreadMethod = GradientSpreadMethod.Reflect;
brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF0000FF"), 0.0));
brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#22FFFFFF"), 0.25));
brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#22FFFFFF"), 0.75));
brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF0000FF"), 1.00));
brush.StartPoint = new Point(0, 0);
brush.EndPoint = new Point(1, 0);

Rectangle rect = new Rectangle();            
rect.Width = 100;
rect.Height = 100;
rect.Fill = brush;
Canvas.SetLeft(rect, 50);
Canvas.SetTop(rect, 50);
MyCanvas.Children.Add(rect);

Rectangle rect2 = new Rectangle();
rect2.Width = 300;
rect2.Height = 100;
rect2.Fill = brush;
Canvas.SetLeft(rect2, 50);
Canvas.SetTop(rect2, 200);
MyCanvas.Children.Add(rect2);

不太对;矩形越大,渐变越宽:

http://imgur.com/CRnXPdV

我尝试将MappingMode设置为Absolute,但这也不起作用。

LinearGradientBrush brush = new LinearGradientBrush();
brush.MappingMode = BrushMappingMode.Absolute;
brush.SpreadMethod = GradientSpreadMethod.Reflect;

brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF0000FF"), 0.0));
brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#22FFFFFF"), 0.25));

brush.StartPoint = new Point(0, 0);
brush.EndPoint = new Point(50, 0);   //I want the gradient to be across 50 pixels

Rectangle rect = new Rectangle();    
//Everything else is the same

这给我一些看起来像这样的东西:

http://imgur.com/EBuxP7D

离开了。 有什么建议吗?

2 个答案:

答案 0 :(得分:2)

实现此目的的一种方法是创建custom controlGrid上用3个矩形组成它;

  • 1列,宽50像素,矩形填充线性渐变
  • 1列“内容”矩形填充剩余部分
  • 1列,宽50像素,矩形填充线性渐变。

答案 1 :(得分:0)

您可以使用将矩形的背景绑定到其ActualHeight并使用转换器,计算该宽度的百分比表示预期的宽度(使用绝对值,您也可以直接设置像素):

class HeightToBrushComplexPropertyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double height = (double)value;
        double percent = 50.0 / height;

        LinearGradientBrush brush = new LinearGradientBrush();
        brush.StartPoint = new Point(0, 0);
        brush.EndPoint = new Point(1, 0);

        brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
        brush.SpreadMethod = GradientSpreadMethod.Pad;

        brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF0000FF"), 0.0));
        brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#22FFFFFF"), percent));
        brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#22FFFFFF"), 1 - percent));
        brush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF0000FF"), 1));

        return brush;

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

对此转换器进行改进:接受像素数和颜色作为参数。