有几种方法可以做到这一点。这是一个。只需扩展Canvas类并在OnRender方法中自己绘制网格即可。
public class GridCanvas : Canvas
{
public int rows = 4;
public int cols = 4;
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
double yStep = this.ActualHeight/rows;
double y = yStep;
for (int i = 0; i < rows - 1; i++)
{
dc.DrawLine(new Pen(Brushes.Black, 1), new Point(0, y), new Point(this.ActualWidth, y));
y += yStep;
}
double xStep = this.ActualWidth/cols;
double x = xStep;
for (int i = 0; i < cols - 1; i++)
{
dc.DrawLine(new Pen(Brushes.Black, 1), new Point(x, 0), new Point(x, this.ActualHeight));
x += xStep;
}
}
}