如何在WPF Canvas上绘制网格线?

Tha*_*.NQ 12 c# algorithm wpf draw

我需要在WPF中在画布上构建一个绘制网格线的函数:

示例网格线

void DrawGridLine(double startX, double startY, double stepX, double stepY, 
                  double slop, double width, double height)
{
    // How to implement draw gridline here?
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Jef*_*ado 30

你不需要用WPF"画"任何东西.如果要绘制线条,请使用适当的几何图形绘制线条.

在你的情况下,它可能很简单.您只是绘制一个网格,因此您可以创建一个DrawingBrush绘制单个网格方块并将其平铺以填充其余网格.要绘制瓷砖,您可以将其视为绘制X的.所以要有一个20x10瓷砖(对应于stepX和stepY):

(ps,slop由于您已经具有水平和垂直步长,因此斜率是多余的)

<DrawingBrush x:Key="GridTile" Stretch="None" TileMode="Tile"
              Viewport="0,0 20,10" ViewportUnits="Absolute">
                  <!-- ^^^^^^^^^^^ set the size of the tile-->
    <DrawingBrush.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Geometry>
                <!-- draw a single X -->
                <GeometryGroup>
                    <!-- top-left to bottom-right -->
                    <LineGeometry StartPoint="0,0" EndPoint="20,10" />

                    <!-- bottom-left to top-right -->
                    <LineGeometry StartPoint="0,10" EndPoint="20,0" />
                </GeometryGroup>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
                <!-- set color and thickness of lines -->
                <Pen Thickness="1" Brush="Black" />
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>
Run Code Online (Sandbox Code Playgroud)

这需要画线.现在,为了能够在网格中从边缘绘制偏移量,您需要使用另一个画笔绘制一个具有所需尺寸的矩形,并填充您的瓷砖.所以具有的起始位置(30, 45)(对应于startX和startY)与width和height,130x120:

<DrawingBrush x:Key="OffsetGrid" Stretch="None" AlignmentX="Left" AlignmentY="Top">
    <DrawingBrush.Transform>
        <!-- set the left and top offsets -->
        <TranslateTransform X="30" Y="45" />
    </DrawingBrush.Transform>
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="{StaticResource GridTile}" >
            <GeometryDrawing.Geometry>
                <!-- set the width and height filled with the tile from the origin -->
                <RectangleGeometry Rect="0,0 130,120" />
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>
Run Code Online (Sandbox Code Playgroud)

然后最后使用它,只需将其设置为网格(或其他面板)的背景:

<Grid Background="{StaticResource OffsetGrid}">
    <!-- ... -->
</Grid>
Run Code Online (Sandbox Code Playgroud)

以下是它最终的结果:

最后看


如果要动态生成画笔,这是基于上述XAML的等效函数:

static Brush CreateGridBrush(Rect bounds, Size tileSize)
{
    var gridColor = Brushes.Black;
    var gridThickness = 1.0;
    var tileRect = new Rect(tileSize);

    var gridTile = new DrawingBrush
    {
        Stretch = Stretch.None,
        TileMode = TileMode.Tile,
        Viewport = tileRect,
        ViewportUnits = BrushMappingMode.Absolute,
        Drawing = new GeometryDrawing
        {
            Pen = new Pen(gridColor, gridThickness),
            Geometry = new GeometryGroup
            {
                Children = new GeometryCollection
                {
                    new LineGeometry(tileRect.TopLeft, tileRect.BottomRight),
                    new LineGeometry(tileRect.BottomLeft, tileRect.TopRight)
                }
            }
        }
    };

    var offsetGrid = new DrawingBrush
    {
        Stretch = Stretch.None,
        AlignmentX = AlignmentX.Left,
        AlignmentY = AlignmentY.Top,
        Transform = new TranslateTransform(bounds.Left, bounds.Top),
        Drawing = new GeometryDrawing
        {
            Geometry = new RectangleGeometry(new Rect(bounds.Size)),
            Brush = gridTile
        }
    };

    return offsetGrid;
}
Run Code Online (Sandbox Code Playgroud)