c# - 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.
解决方案
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();
}
}
推荐阅读
- ios - 即使添加了视图,仍会收到 UIViewContorller 的触摸事件
- java - 从特定索引到最后一行计算 excel 行 - Apache POI
- linux - 给定用户计算他们的进程消耗了多少字节
- c++ - 模板推导中的 const T & 和 T & 有什么区别
- c++ - 为什么在某些情况下使用友元运算符<<,而在其他情况下不使用
- python - 在散景中包含聚集条形图的工具提示
- reactjs - 使用 React 以编程方式返回主页
- perl - Tcl 脚本还是 Perl?
- java - 如何在 JVM 11 中运行带有 jre 8 类的 java 11 编译程序
- python - 使用 sklearn2pmml 阅读 pmml?