Click here to Skip to main content
13,353,462 members (38,859 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 17 Jun 2012

LinesGrid control - An easy way to draw a grid in WPF

, 13 Nov 2012
Rate this:
Please Sign up or sign in to vote.
The article describes how to draw a grid of vertical and horizontal lines in WPF.


The solution contains a LinesGrid control WPF project, and a sample WPF application showing how to use it.


This control appeared as I needed to draw certain amount of horizontal and vertical lines with equal intervals on top of another controls. I decided to post it online, as it might be useful for some of you.

Using the code

Basically, the control is rather simple. It consists of two Grids, one on top of another. The first Grid  is used to draw the vertical (from top to bottom) lines, while the second - the horizontal lines. Each of the Grids' Background properties is set to a VisualBrush containing a tile with a black pixel. 

<Grid Name="LayoutRoot">
        <VisualBrush x:Name="verticalLines" Viewport="0,0,0.2,1" 

                    TileMode="Tile" Viewbox="0,0,2,1">
                    <Line Stroke="Black" StrokeThickness="1" X1="1" />
        <VisualBrush x:Name="horizontalLines" Viewport="0,0,1,0.1" 

                   TileMode="Tile" Viewbox="0,0,1,30">
                    <Line Stroke="Black" StrokeThickness="1" X1="1" />

The control has four dependency properties declared: Columns, Rows, LineThickness and LineBrush. While with the first two you can set the quantity of lines to draw vertically and horizontally, the  LineThickness property allows to set the thickness of the lines, and LineBrush defines the brush used to draw them.  

The  RecreateLines method is used to calculate the needed  height and width of the VisualBrushes' Viewports. In this method we also calculate the Viewboxes' Height and Width (depending on the lines thickness needed):

private void RecreateLines()
    if (this.ActualWidth == 0 || this.ActualHeight == 0)

    double cellWidth = this.ActualWidth / (this.LineThickness * this.Columns);
    this.verticalLines.Viewbox = new Rect(0, 0, cellWidth, 1);

    double cellHeight = this.ActualHeight / (this.LineThickness * this.Rows);
    this.horizontalLines.Viewbox = new Rect(0, 0, 1, cellHeight);

    double qv = (1d - this.LineThickness / this.ActualWidth) / this.Columns;
    this.verticalLines.Viewport = new Rect(0, 0, qv, 1);

    double qh = (1d - this.LineThickness / this.ActualHeight) / this.Rows;
    this.horizontalLines.Viewport = new Rect(0, 0, 1, qh);

The method is called on Rows or Cells value change.


There also might be some better ways to accomplish drawing a grid of lines in WPF, though for my primary purpose (debugging) the way described in this article was enough. 

P. S.

This is my first article on codeproject, so any responses are highly appreciated and welcome! Smile | <img src= 

Thank you!  


  • The HorizontalLines and VerticalLines properties renamed to more obvious Rows and Columns
  • Updated the RecreateLines method - now the bottommost and rightmost lines are also drawn. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionBinding Pin
TonyVFish9-Nov-15 2:46
memberTonyVFish9-Nov-15 2:46 
QuestionSimilar solution Pin
kasohn19-Jun-12 0:34
memberkasohn19-Jun-12 0:34 
GeneralMy vote of 5 Pin
sam.hill17-Jun-12 17:23
membersam.hill17-Jun-12 17:23 
GeneralRe: My vote of 5 Pin
maximaximum17-Jun-12 21:39
membermaximaximum17-Jun-12 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 13 Nov 2012
Article Copyright 2012 by maximaximum
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid