有问题。我建立了一个DrawingBrush,它的绘图是一个DrawingGroup,组的孩子可以改变。所以我在后台代码中构建它。这幅图。画笔是RadialGradientBrush。
我添加了5张图到群组中,但是它只能显示最后一张。但是xaml可以显示所有这些。我发现这是我的RadialGradientBrush的问题,但我找不到我的代码的错误。你能帮我吗?非常感谢,这是我的代码。
<Rectangle Name="rect">
<Rectangle.Fill>
<DrawingBrush x:Name="totalBrush">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing x:Name="geometry1">
<GeometryDrawing.Brush>
<RadialGradientBrush x:Name="Brush1" GradientOrigin="0.2,0.2" Center="0.2,0.2" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush x:Name="Brush2" GradientOrigin="0.8,0.2" Center="0.8,0.2" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush x:Name="Brush3" GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush x:Name="Brush4" GradientOrigin="0.2,0.8" Center="0.2,0.8" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush x:Name="Brush5" GradientOrigin="0.8,0.8" Center="0.8,0.8" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
private void InitializeTemperaturePoints()
{
DrawingGroup group = new DrawingGroup();
DrawingBrush drawingBrush = new DrawingBrush();
var drawing1 = GenerateDrawing(0.2, 0.2);
var drawing2 = GenerateDrawing(0.8, 0.2);
var drawing3 = GenerateDrawing(0.5, 0.5);
var drawing4 = GenerateDrawing(0.2, 0.8);
var drawing5 = GenerateDrawing(0.8, 0.8);
//var drawing1 = GenerateDrawing(1);
//var drawing2 = GenerateDrawing(2);
//var drawing3 = GenerateDrawing(3);
//var drawing4 = GenerateDrawing(4);
//var drawing5 = GenerateDrawing(5);
group.Children.Add(drawing1);
group.Children.Add(drawing2);
group.Children.Add(drawing3);
group.Children.Add(drawing4);
group.Children.Add(drawing5);
drawingBrush.Drawing = group;
rect1.Fill = drawingBrush;
}
GeometryDrawing GenerateDrawing(double x, double y)
{
RadialGradientBrush brush = new RadialGradientBrush();
brush.GradientOrigin = new Point(x, y);
brush.Center = new Point(x, y);
brush.RadiusX = 0.5;
brush.RadiusY = 0.5;
GradientStop stop1 = new GradientStop(Colors.Red, 0);
GradientStop stop2 = new GradientStop(Colors.Blue, 1);
if (brush.GradientStops == null)
{
brush.GradientStops = new GradientStopCollection();
}
brush.GradientStops.Add(stop1);
brush.GradientStops.Add(stop2);
brush.Opacity = 1;
GeometryDrawing drawing = new GeometryDrawing();
Rect rectangle = new Rect(0, 0, 1, 1);
RectangleGeometry geometry = new RectangleGeometry(rectangle);
drawing.Geometry = geometry;
drawing.Brush = brush;
return drawing;
}
GeometryDrawing GenerateDrawing(int index)
{
Brush brush = null;
switch (index)
{
case 1: brush = Brush1; break;
case 2: brush = Brush2; break;
case 3: brush = Brush3; break;
case 4: brush = Brush4; break;
case 5: brush = Brush5; break;
}
GeometryDrawing drawing = new GeometryDrawing();
Rect rectangle = new Rect(0, 0, 1, 1);
RectangleGeometry geometry = new RectangleGeometry(rectangle);
drawing.Geometry = geometry;
drawing.Brush = brush;
return drawing;
}
}
笔刷之间的区别在于XAML中的笔刷是部分透明的:
<RadialGradientBrush x:Name="Brush1"
GradientOrigin="0.2,0.2"
Center="0.2,0.2"
RadiusX="0.5"
RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFF0000" Offset="0" />
<GradientStop Color="#000000FF" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
代码中的笔刷没有导致最后一个矩形覆盖其他矩形:
GradientStop stop1 = new GradientStop(Colors.Red, 0);
GradientStop stop2 = new GradientStop(Colors.Blue, 1);
修改:
GradientStop stop1 = new GradientStop(Colors.Red, 0);
GradientStop stop2 = new GradientStop(Color.FromArgb(0, 0, 0, 1), 1);