首页 > 解决方案 > How to fill the rectangle with some colors, using gradient but only as a tool(without visible gradient)?

问题描述

I'm making converter for the "Fill" property of the rectangle to fill it with different colors.

So I have a list of info about dates of absence of a person. Different parts of rectangle must be filled with color1 if person was absent in a period, and with color2 otherwise. For each person in FOR I compute difference between dates and fill the gradient stops like: color1-border-color2-border-color1... I try to make border almost invisible.

Converter:

 double bufferOffset = 0.0;
 double taskPeriodInDays = (endDate.Value.Date - 
 startDate.Value.Date).TotalDays;
 List<double> timeSections = new List<double>();

 for (int i = 1; i < absencesDatesBetweenStartAndEnd.Count; i++)
    {
       IsAbsent = !IsAbsent;
       GradientStop color = new GradientStop();
       GradientStop border = new GradientStop();
       border.Color = Colors.Black;

   color.Color = IsAbsent ? Colors.Orange : Colors.Violet;


   color.Offset = i == 1 ? 0 : bufferOffset;
   gradient.GradientStops.Add(color);

   color.Offset = bufferOffset + (absencesDatesBetweenStartAndEnd[i].Date - absencesDatesBetweenStartAndEnd[i - 1].Date).TotalDays / taskPeriodInDays;
   gradient.GradientStops.Add(color);

  if(i == absencesDatesBetweenStartAndEnd.Count - 1)
   {
      break;
   }

        border.Offset = color.Offset + 0.00001;
        gradient.GradientStops.Add(border);

        border.Offset = border.Offset + 0.00001;
        gradient.GradientStops.Add(border);

        bufferOffset = border.Offset;
   }
   return gradient;

<Rectangle Fill="{Binding Converter={StaticResource TaskExecutorAbsenceColorConverter}}"/>

Hardcoding XAML for trying values:

<Rectangle.Fill>
   <LinearGradientBrush StartPoint="0 0" EndPoint="1 0">
      <GradientStop Color="red" Offset="0"/>
      <GradientStop Color="red" Offset="0.065573770491803282"/>
      <GradientStop Color="black" Offset="0.065583770491803278"/>
      <GradientStop Color="black" Offset="0.065593770491803274"/>
      <GradientStop Color="blue" Offset="0.065593770491803274"/>
      <GradientStop Color="blue" Offset="0.29510196721311477"/>
      <GradientStop Color="black" Offset="0.29511196721311478"/>
      <GradientStop Color="black" Offset="0.29512196721311479"/>
      <GradientStop Color="red" Offset="0.29512196721311479"/>
      <GradientStop Color="red" Offset="0.36069573770491808"/>
      <GradientStop Color="black" Offset="0.36070573770491809"/>
      <GradientStop Color="black" Offset="0.3607157377049181"/>
      <GradientStop Color="blue" Offset="0.3607157377049181"/>
      <GradientStop Color="blue" Offset="0.50825672131147548"/>
      <GradientStop Color="black" Offset="0.50826672131147543"/>
      <GradientStop Color="black" Offset="0.50827672131147539"/>
      <GradientStop Color="red" Offset="0.50827672131147539"/>
      <GradientStop Color="red" Offset="1.00008"/>
   </LinearGradientBrush>
</Rectangle.Fill>

The converter works incorrect. The gradient is visible.

As you can see, I've tried to put the values of gradientstops into XAML, in this case it works great. I don't understand why.

Hardcoding values in XAML Using converter

标签: c#wpfxamlgradientconverters

解决方案


Probably the most appropriate way to go is to use ItemsControl with colored rectangles. But if you still want a brush you can use a visual brush instead like this:

public class Conv : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        VisualBrush result = new VisualBrush();
        result.Stretch = Stretch.Fill;
        StackPanel container = new StackPanel();
        container.Orientation = Orientation.Horizontal;

        //Generate a colored rectangle for each day.
        //I just use some random values for illustration.
        Random rnd = new Random();
        for (int i = 0; i < 10; i++)
        {
            Rectangle rect = new Rectangle();
            rect.Width = 20;
            rect.Height = 20;
            rect.Fill = rnd.Next() % 2 == 0 ? Brushes.Red : Brushes.Green;
            container.Children.Add(rect);
        }
        result.Visual = container;
        return result;
    }

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

推荐阅读