Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips / View Tip

Tip: How to add control into a specific cell of a Grid control?

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


Let's imagine we would like to add a button at a specific cell (row:column) in our grid control. How this could be done? How to define the row and column of that cell? Well, the answer is pretty easy - you just have to define values for the two attached properties Grid.Row and Grid.Column inside the control you wish to add. Like this:

Xaml

<Grid x:Name="MyGrid" ShowGridLines="True">
   <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
      <ColumnDefinition></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
   </Grid.ColumnDefinitions>
   <Button x:Name="MyButton" Content="Button at Cell(R:0, C:1)" Grid.Row="0" Grid.Column="1"></Button>
</Grid>

C# 

Grid MyGrid = new Grid();
 
RowDefinition row1 = new RowDefinition();
RowDefinition row2 = new RowDefinition();
MyGrid.RowDefinitions.Add( row1 );
MyGrid.RowDefinitions.Add( row2 );
 
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
MyGrid.ColumnDefinitions.Add( col1 );
MyGrid.ColumnDefinitions.Add( col2 );
 
Button MyButton = new Button();
MyButton.SetValue( Grid.RowProperty, 0 );
MyButton.SetValue( Grid.ColumnProperty, 1 );

 

Do you see the row before the last one, the row where we add the button? OK, if you see it, then you also probably notice the two properties Grid.Row="0" Grid.Column="1". Using these attached properties you actually define the exact row and column where your control should be inserted to. And last but not least, don't forget that the row and column indexes are zero based.

That's it!

 

Share


Comments

Comments RSS RSS
  • RE: How to add control into a specific cell of a Grid control?  

    posted by Enver on Sep 03, 2008 03:55

     Could you show us how to do the same in code!

  • RE: How to add control into a specific cell of a Grid control?  

    posted by iiordanov on Sep 03, 2008 07:58

    Hi Enver,

    The tip was updated with the C# code that allows you to do the same.

  • RE: Tip: How to add control into a specific cell of a Grid control?  

    posted by Enver on Sep 04, 2008 06:32

    Cheers, that answers my query!

  • RE: Tip: How to add control into a specific cell of a Grid control?  

    posted by Enver on Sep 04, 2008 07:35

    Also I have to add a that you must also add the following line  just before SetValue(...)

    MyGrid.Children.Add(MyButton);

    Cheers!

     

  • RE: Tip: How to add control into a specific cell of a Grid control?  

    posted by iiordanov on Sep 04, 2008 07:43

    10x, you are absolutely right! We will update the tip. :)

Add Comment

 
 

   
  
  
   
Please add 7 and 6 and type the answer here:

Welcome to SilverlightShow - the portal for Silverlight resources. You can find here daily news (RSS), articles authored exclusively for SilverlightShow (RSS), showcases, tips, books, and much more! Follow us on twitter.
Join the discussions and sharing of experience by becoming a member. Or, find out the top 10 reasons to sign up. Thanks for stopping by! (hide this)