user1512186
user1512186

Reputation: 397

Adding Grids In codeBehind

To divide a Grid into rows we give it row definitions and the UIElement that needs to be placed in a particular row in that grid we do it like this:

Button Name="Button1" Grid.Row="1"

Now suppose I want to do this thing in code behind dynamically then how can we do it.

Thank You.

Upvotes: 2

Views: 2727

Answers (2)

Deeko
Deeko

Reputation: 1539

You can do everything from code behind that you can from XAML regarding grids:

Grid.SetRow(button, 1);
Grid.SetColumn(button, 1);
Grid.SetRowSpan(button, 2);
Grid.SetColumnSpan(button, 2);

myGrid.ColumnDefinitions.Add(new ColumnDefinition());
myGrid.RowDefinitions.Add(new RowDefinition());

etc...

Upvotes: 3

dkozl
dkozl

Reputation: 33394

If you want to create a Button in code and add to specific cell of the Grid then you can do it like this:

var myButton = new Button();
myButton.Content = "myButton";
Grid.SetColumn(myButton, 1);
Grid.SetRow(myButton, 1);
myGrid.Children.Add(myButton);

Upvotes: 4

Related Questions