[WPF]如何在画布上绘制网格? [英] [WPF]How to draw a grid on canvas?

查看:68
本文介绍了[WPF]如何在画布上绘制网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在继承自Canvas-系统ui元素的自定义画布上绘制以下图表作为背景?

How to draw the following chart as a background on custom canvas inherited from Canvas - system ui element?

感谢任何有用的链接.

推荐答案

您只需将 Canvas.Background 设置为某些 DrawingBrush .该笔刷只需要渲染一个Rectangle(通过使用一些 RectangleGeometry )即可.由于支持 TileMode ,我们可以沿水平轴和垂直轴重复此矩形,并为您制作完整的网格:

You can just set Canvas.Background to some DrawingBrush. This brush can just need to render a Rectangle (by using some RectangleGeometry). Because of supporting for TileMode, we can repeat this rectangle along both horizontal and vertical axes and make the full grid for you:

<Canvas>
    <Canvas.Background>
       <DrawingBrush TileMode="Tile" Viewport="-10,-10,40,40" 
                                     ViewportUnits="Absolute">
          <DrawingBrush.Drawing>
             <GeometryDrawing>                 
                <GeometryDrawing.Geometry>
                   <RectangleGeometry Rect="0,0,50,50"/>
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                   <Pen Brush="Gray" Thickness="1"/>
                </GeometryDrawing.Pen>
             </GeometryDrawing>
          </DrawingBrush.Drawing>
       </DrawingBrush>
    </Canvas.Background>
 </Canvas>

请注意,您可以在画布"外部绘制某些内容,但是其背景"始终位于其区域之内.因此,您需要正确设置画布"的大小.

Note that you can draw something outside the Canvas but its Background is always inside its region. So you need to set the Size for your Canvas correctly.

这篇关于[WPF]如何在画布上绘制网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆