在WPF中创建对角线图案 [英] Creating Diagonal Pattern in WPF
问题描述
我想在WPF中创建对角阴影图案。我正在使用以下XAML代码生成它:
I want to create diagonal hatch pattern in WPF. I am using following XAML code to generate it:
<VisualBrush
x:Key="HatchBrushnew"
TileMode="Tile" Viewport="0,0,30,30"
ViewportUnits="Absolute" Viewbox="0,0,30,30"
ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Canvas>
<Path Stroke="Gray" StrokeThickness="0.1cm" >
<Path.Data>
<LineGeometry StartPoint="0,0" EndPoint="30,30" />
</Path.Data>
</Path>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
但是在用这种模式填充形状之后,两条线之间的间隙很小。有人可以建议一种避免这种微小差距的方法吗?
But the after filling shape with this pattern, I am getting small gap between two lines. Can anyone suggest a way to avoid that small gap?
推荐答案
一个 DrawingBrush
比一个简单得多。 VisualBrush
。
A DrawingBrush
would be much simpler than a VisualBrush
.
除了中央对角线外,它还绘制了另外两条线(当然,这可能是较短)以覆盖画笔图块的右上角和左下角:
In addition to the central diagonal line, this one draws two additional lines (which may of course be shorter) to cover the top right and bottom left corners of the Brush tile:
<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>
如Balázs给出的答案所示,您也可以设置Brush的 Transform
属性,并使用例如单个垂直 LineGeometry
:
As shown in the answer given by Balázs, you may also set the Brush's Transform
property, and use e.g. a single vertical LineGeometry
:
<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
Viewport="0,0,30,30" ViewportUnits="Absolute"
Viewbox="0,0,30,30" ViewboxUnits="Absolute">
<DrawingBrush.Transform>
<RotateTransform Angle="45"/>
</DrawingBrush.Transform>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Black" Thickness="5"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<LineGeometry StartPoint="0,15" EndPoint="30,15"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
这篇关于在WPF中创建对角线图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!