如何在WPF画布上绘制网格线?

13

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

示例网格线

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

我该怎么做?

1个回答

32
你不必使用WPF来“绘制”任何东西。如果想要画线条,可以使用适当的几何图形进行绘制。
在你的情况下,这很简单。你只是在画一个网格,所以你可以创建一个DrawingBrush来绘制单个网格正方形,并将其平铺以填充剩余部分。为了画出你的正方形,你可以把它看作是画X的过程。因此,要有一个20x10的瓷砖(对应于stepX和stepY):
(p.s.,斜率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>

这样就完成了线条的绘制。现在为了能够从网格边缘偏移地绘制它们,您需要有另一个画笔,在其中绘制具有所需尺寸的矩形,并填充您的图块。因此,要使用起始位置(30, 45)(对应于startXstartY)以及widthheight130x120

<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>

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

<Grid Background="{StaticResource OffsetGrid}">
    <!-- ... -->
</Grid>

最终效果如下所示:

final look


如果您想要动态生成画刷,则可以使用基于上述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;
}

这段代码非常棒,感谢分享。但是关于C#动态创建等效代码,我有一些问题 - 我似乎无法使用它来创建画刷,因为存在抽象类继承问题。只是好奇你会如何通过这种方法创建一个画刷? - WearyWanderer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接