Click here to Skip to main content
15,352,714 members
Articles / Desktop Programming / WPF
Posted 17 Mar 2010


7 bookmarked

Beginner WPF Panels Grid with TextBlocks Example

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
17 Mar 2010CPOL2 min read
How to create TextBlocks inside a WPF Grid


I am in the process of teaching myself WPFXAML and basically general .NET development. Although I am not a professional developer, my goal is to become a good developer one day. This is my first article and it shows how one can use XAML to create grids in a WPF panel. A Grid is probably the most extensible panel available. A Grid gives the developer control over both rows and columns. One can consider a Grid to be similar to a database table, a data grid control in a WinForms application or maybe an HTML Table, but Grids work differently than tables. Grids are probably a better setup than their table counterpart. The screenshot below is what the end product will look like. 

Creating the Grid!

A Grid is composed of rows and columns similar to a table. We first start by declaring the Grid element. Then we declare a set of four RowDefinitions without any properties, and then we declare three ColumnDefinitions the same way we declared the RowDefinitions, without any properties. See the code snippet below:

<Window x:Class="WPFPanels_Grid.Window1"
Title="Sundeepan's Panel Grids Example" Width="356">

After the Rows and Columns are defined, we declare the TextBlocks. Below is the code for the first TextBlock:

<TextBlock TextBlock.FontSize="36"

Let's see what we did in the TextBlock declaration. First we specified the FontSize to be 36, this size will reflect on the number displayed in the TextBlock (refer to the screen shot). Then we set the Foreground to the color Peru and the Background to Bisque. The row and column position on the grid is also declared. We set the first TextBlock to the Column 0 (the first column) and Row 0 (the first row), and this TextBlock will span 2 columns declared by the RowSpan property. After all this is done, moving forward we set the text for the TextBlock to 1. Below is a code snippet that demonstrates the ColumnSpan attribute for TextBlock number 8. ColumnSpan is almost the same as a RowSpan except this TextBlock will span 3 columns, it's pretty much self explanatory.

<TextBlock TextBlock.FontSize="36"

So the code declares that the first TextBlock will show the number "1" with a font size of 36 with the foreground color as Peru and the background color as Bisque. The placement of the block will be in column 0, row 0 of the grid spanning two rows. Keep in mind that the grid has no colors. It is simply supplying the configuration, the TextBlocks inside the grids provide all the attributes.


  • 17th March, 2010: Initial post


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


About the Author

Sundeepan Sen
Systems Engineer
United States United States
Sundeepan Sen has been involved with technology for as long as he can remember. He got his start with the internet as a sophomore in high school in 1995 at the age of 15 and hasn't stopped since. His first encounter with the Internet was on a 2400 baud modem on a VAX/VMS shell account. The first program he wrote is in assembly for a game called "Core War" it can be found here:

He likes all things technology and loves to learn anything that comes his way.

Sundeepan is also artistically gifted with music, graphics, drawing and poetry.

He is a Network Consultant providing services with the latest Microsoft technologies.

Comments and Discussions

GeneralWPF Grid panel Pin
WPFAdmin18-Jan-13 23:40
MemberWPFAdmin18-Jan-13 23:40 
GeneralMy vote of 5 Pin
FluffyDucktheLegend29-Oct-10 0:59
MemberFluffyDucktheLegend29-Oct-10 0:59 
GeneralMy vote of 1 Pin
Dan Danz25-Mar-10 8:31
MemberDan Danz25-Mar-10 8:31 
Already very well covered in documentation and in several books on WPF.
GeneralRe: My vote of 1 Pin
FluffyDucktheLegend29-Oct-10 0:59
MemberFluffyDucktheLegend29-Oct-10 0:59 
GeneralMy vote of 2 Pin
John Brett17-Mar-10 5:47
MemberJohn Brett17-Mar-10 5:47 
GeneralRe: My vote of 2 Pin
Sundeepan Sen17-Mar-10 6:36
MemberSundeepan Sen17-Mar-10 6:36 
GeneralMy vote of 1 Pin
jimmson17-Mar-10 4:00
Memberjimmson17-Mar-10 4:00 
GeneralRe: My vote of 1 Pin
Sundeepan Sen17-Mar-10 7:23
MemberSundeepan Sen17-Mar-10 7:23 
GeneralRe: My vote of 1 [modified] Pin
eroszamp5-Nov-10 6:19
Membereroszamp5-Nov-10 6:19 

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.