使用 WPF 在代码隐藏中创建线型
Creating line pattern in code behind with WPF
我正在尝试将以下 XAML 代码转换为代码隐藏版本:
<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
Viewport="0,0,30,30" ViewportUnits="Absolute"
Viewbox="0,0,30,30" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Black" Thickness="5"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<Geometry>M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45</Geometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
这就是我所做的:
private DrawingBrush GetDBrush()
{
DrawingBrush d = new DrawingBrush() { TileMode = TileMode.Tile, Viewport = new Rect(0, 0, 30, 0), ViewportUnits = BrushMappingMode.Absolute, Viewbox = new Rect(0, 0, 30, 0), ViewboxUnits = BrushMappingMode.Absolute };
Brush penBrush = new SolidColorBrush(Colors.Black);
penBrush.Freeze();
Pen pen = new Pen(penBrush, 0.5); pen.Freeze();
Rect r = new Rect(0, 0, 100, 30);
Geometry g = new RectangleGeometry(r);
GeometryDrawing drawing = new GeometryDrawing(new SolidColorBrush(Colors.BlueViolet), pen, g);
d.Drawing = drawing;
var dGroup = new DrawingGroup();
using (DrawingContext dc = dGroup.Open())
{
dc.DrawGeometry(penBrush, null, Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45"));
}
return d;
}
我无法设置背景颜色,基本上我会设置带有黑色弯曲线条的白色背景。我的代码有什么问题?
在 XAML 中声明的 DrawingBrush 的精确副本是这样的:
private DrawingBrush GetDBrush()
{
return new DrawingBrush
{
TileMode = TileMode.Tile,
Viewport = new Rect(0, 0, 30, 30),
ViewportUnits = BrushMappingMode.Absolute,
Viewbox = new Rect(0, 0, 30, 30),
ViewboxUnits = BrushMappingMode.Absolute,
Drawing = new GeometryDrawing
{
Pen = new Pen(Brushes.Black, 5),
Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
}
};
}
为了也有背景颜色,您需要另一个绘图:
private DrawingBrush GetDBrush()
{
var backgroundDrawing = new GeometryDrawing
{
Brush = Brushes.Yellow,
Geometry = new RectangleGeometry(new Rect(0, 0, 45, 45))
};
var foregroundDrawing = new GeometryDrawing
{
Pen = new Pen(Brushes.Black, 5),
Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
};
var drawing = new DrawingGroup();
drawing.Children.Add(backgroundDrawing);
drawing.Children.Add(foregroundDrawing);
return new DrawingBrush
{
TileMode = TileMode.Tile,
Viewport = new Rect(0, 0, 30, 30),
ViewportUnits = BrushMappingMode.Absolute,
Viewbox = new Rect(0, 0, 30, 30),
ViewboxUnits = BrushMappingMode.Absolute,
Drawing = drawing
};
}
我正在尝试将以下 XAML 代码转换为代码隐藏版本:
<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
Viewport="0,0,30,30" ViewportUnits="Absolute"
Viewbox="0,0,30,30" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Black" Thickness="5"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<Geometry>M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45</Geometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
这就是我所做的:
private DrawingBrush GetDBrush()
{
DrawingBrush d = new DrawingBrush() { TileMode = TileMode.Tile, Viewport = new Rect(0, 0, 30, 0), ViewportUnits = BrushMappingMode.Absolute, Viewbox = new Rect(0, 0, 30, 0), ViewboxUnits = BrushMappingMode.Absolute };
Brush penBrush = new SolidColorBrush(Colors.Black);
penBrush.Freeze();
Pen pen = new Pen(penBrush, 0.5); pen.Freeze();
Rect r = new Rect(0, 0, 100, 30);
Geometry g = new RectangleGeometry(r);
GeometryDrawing drawing = new GeometryDrawing(new SolidColorBrush(Colors.BlueViolet), pen, g);
d.Drawing = drawing;
var dGroup = new DrawingGroup();
using (DrawingContext dc = dGroup.Open())
{
dc.DrawGeometry(penBrush, null, Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45"));
}
return d;
}
我无法设置背景颜色,基本上我会设置带有黑色弯曲线条的白色背景。我的代码有什么问题?
在 XAML 中声明的 DrawingBrush 的精确副本是这样的:
private DrawingBrush GetDBrush()
{
return new DrawingBrush
{
TileMode = TileMode.Tile,
Viewport = new Rect(0, 0, 30, 30),
ViewportUnits = BrushMappingMode.Absolute,
Viewbox = new Rect(0, 0, 30, 30),
ViewboxUnits = BrushMappingMode.Absolute,
Drawing = new GeometryDrawing
{
Pen = new Pen(Brushes.Black, 5),
Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
}
};
}
为了也有背景颜色,您需要另一个绘图:
private DrawingBrush GetDBrush()
{
var backgroundDrawing = new GeometryDrawing
{
Brush = Brushes.Yellow,
Geometry = new RectangleGeometry(new Rect(0, 0, 45, 45))
};
var foregroundDrawing = new GeometryDrawing
{
Pen = new Pen(Brushes.Black, 5),
Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
};
var drawing = new DrawingGroup();
drawing.Children.Add(backgroundDrawing);
drawing.Children.Add(foregroundDrawing);
return new DrawingBrush
{
TileMode = TileMode.Tile,
Viewport = new Rect(0, 0, 30, 30),
ViewportUnits = BrushMappingMode.Absolute,
Viewbox = new Rect(0, 0, 30, 30),
ViewboxUnits = BrushMappingMode.Absolute,
Drawing = drawing
};
}