Skip Navigation LinksHome / Tips / View Tip

Tip: How to define the count of rows and columns in a Grid control?

+ Add to SilverlightShow Favorites
0 comments   /   posted by Denislav Savkov on Sep 04, 2008
(0 votes)
Categories: Controls and UI

The System.Windows.Controls.Grid control is one of the most commonly used containers in Silverlight. It allows you to organize its content into rows and columns as a regular grid. By default a Grid control has only one cell and everything you add goes there overlapping the previous content. In order to define multiple rows and columns to your Grid control you have to add as many <RowDefinition> and <ColumnDefinition> as you need, see the sample below.

Xaml

<Page
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid x:Name="MyGrid" ShowGridLines="True">
     <Grid.RowDefinitions>
       <RowDefinition></RowDefinition>
       <RowDefinition></RowDefinition>
       <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
       <ColumnDefinition></ColumnDefinition>
       <ColumnDefinition></ColumnDefinition>
       <ColumnDefinition></ColumnDefinition>
     </Grid.ColumnDefinitions>
   </Grid>
</Page>

C#

Grid MyGrid = new Grid();
 
RowDefinition row1 = new RowDefinition();
RowDefinition row2 = new RowDefinition();
RowDefinition row3 = new RowDefinition();
MyGrid.RowDefinitions.Add( row1 );
MyGrid.RowDefinitions.Add( row2 );
MyGrid.RowDefinitions.Add( row3 );
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
ColumnDefinition col3 = new ColumnDefinition();
MyGrid.ColumnDefinitions.Add( col1 );
MyGrid.ColumnDefinitions.Add( col2 );
MyGrid.ColumnDefinitions.Add( col3 );

As you can see we have added 3 rows and 3 columns definitions in our Grid control. So finally what we get is a grid with 9 cells ordered 3x3.

That's it!

 

Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 1 and 1 and type the answer here:

Watch the recording of the latest SilverlightShow webcast 'Running Silverlight Outside the Browser and with Elevated Trust'.
Join our next live webinar session by Braulio Diez - Sketchflow in Real Scenarios, on Sept 29th, 8am - 9am PDT.
Learn More | Sign Up | More Webinars by SilverlightShow (hide this)