(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

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

(0 votes)
Denislav Savkov
>
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
5 comments   /   posted on Sep 03, 2008
Tags:   grid , denislav-savkov
Categories:   Controls


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!

 


Subscribe

Comments

  • -_-

    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!

  • iiordanov

    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!

     

  • iiordanov

    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

Login to comment:
  *      *       
Login with Facebook