3

我想使用 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

这是很遥远的事情。有什么建议么?

4

2 回答 2

2

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

  • 1 列,50 像素宽,矩形填充线性渐变
  • 1 列“内容”矩形填充剩余部分
  • 1 列,50 像素宽,带有一个填充有线性渐变的矩形。
于 2013-02-19T17:16:24.200 回答
1

您可以使用将矩形的背景绑定到其 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();
    }
}

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

于 2013-11-14T14:50:40.973 回答