I need to build a function drawing gridline on the canvas in WPF:
void DrawGridLine(double startX, double startY, double stepX, double stepY,
double slop, double width, double height)
{
// How to implement draw gridline here?
}
How would I go about this?
You don't really have to "draw" anything with WPF. If you want to draw lines, use the appropriate geometries to draw them.
In your case it could be simple really. You're just drawing a grid so you could just create a
DrawingBrush
to draw a single grid square and tile it to fill in the rest. To draw your tile, you could think of it as drawing X's. So to have a20x10
tile (which corresponds tostepX
andstepY
):(p.s., the slope
slop
is redundant since you already have the horizontal and vertical step sizes)That takes care of drawing the lines. Now to be able to draw them offset in your grid from the edges, you need to have another brush where you draw a rectangle with the desired dimensions, filled with your tiles. So to have a starting position of
(30, 45)
(corresponding tostartX
andstartY
) with thewidth
andheight
,130x120
:Then finally to use it, just set it as the background of your grid (or other panel):
Here's how it ends up looking like:
If you want to generate the brush dynamically, here's an equivalent function based on the above XAML: