使用直线而不是矩形在Canvas上绘制网格图案

时间:2017-07-28 08:42:06

标签: c# wpf xaml canvas

现在我用这个画笔绘制网格:

<VisualBrush x:Key="GridBrush" TileMode="Tile"
        Viewport="0,0,60,60" ViewportUnits="Absolute"
        Viewbox="0,0,60,60" ViewboxUnits="Absolute" >
    <VisualBrush.Visual>
        <Rectangle Stroke="Darkgray" StrokeThickness="1" Height="60" Width="60"
            StrokeDashArray="5 3"/>
    </VisualBrush.Visual>
</VisualBrush>

我得到了这个效果:

overalapping

这里的矩形重叠。

我必须要做的是:

wanted result

1 个答案:

答案 0 :(得分:2)

而不是VisualBrush,更好地使用此DrawingBrush:

<DrawingBrush TileMode="Tile"
              Viewport="0,0,60,60" ViewportUnits="Absolute"
              Viewbox="0,0,60,60" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing Geometry="M0,0 L60,0 M0,0 L0,60">
            <GeometryDrawing.Pen>
                <Pen Brush="DarkGray" Thickness="1" DashCap="Flat">
                    <Pen.DashStyle>
                        <DashStyle Dashes="5,3"/>
                    </Pen.DashStyle>
                </Pen>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

您也可以明确地将Geometry声明为一组两行:

<DrawingBrush TileMode="Tile"
        Viewport="0,0,60,60" ViewportUnits="Absolute"
        Viewbox="0,0,60,60" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Geometry>
                <GeometryGroup>
                    <LineGeometry EndPoint="0,60"/>
                    <LineGeometry EndPoint="60,0"/>
                </GeometryGroup>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
                <Pen Brush="DarkGray" Thickness="1" DashCap="Flat">
                    <Pen.DashStyle>
                        <DashStyle Dashes="5,3"/>
                    </Pen.DashStyle>
                </Pen>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>