I want to add a grid and its contents on runtime. The Grid is as follows. How can I add this from Code Behind?
<Grid Width="200" Height="50" Margin="0,50,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Name="chkBox" MinWidth="32" HorizontalAlignment="Left" Background="Transparent"/>
<TextBlock Grid.Column="1" Text="Name" FontSize="16" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<TextBox Grid.Column="2" Name="txtBx" FontSize="16" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
Grid grid = new Grid();
grid.Width = 200;
grid.Height = 50;
grid.Margin = new Thickness(0, 50, 0, 0);
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
ColumnDefinition col3 = new ColumnDefinition();
col1.Width = new GridLength(0, GridUnitType.Auto);
col2.Width = new GridLength(0, GridUnitType.Auto);
col3.Width = new GridLength(1, GridUnitType.Star);
grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2);
grid.ColumnDefinitions.Add(col3);
CheckBox cbox = new CheckBox();
cbox.MinWidth = 32;
cbox.HorizontalAlignment = HorizontalAlignment.Left;
cbox.Background = new SolidColorBrush(Windows.UI.Colors.Transparent);
TextBlock tblock = new TextBlock();
tblock.FontSize = 16;
tblock.HorizontalAlignment = HorizontalAlignment.Left;
tblock.VerticalAlignment = VerticalAlignment.Center;
tblock.Text = "text";
TextBox tbox = new TextBox();
tbox.FontSize = 16;
tbox.HorizontalAlignment = HorizontalAlignment.Left;
tbox.VerticalAlignment = VerticalAlignment.Center;
grid.Children.Add(cbox);
grid.Children.Add(tblock);
grid.Children.Add(tbox);
Grid.SetColumn(cbox, 0);
Grid.SetColumn(tblock, 1);
Grid.SetColumn(tbox, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With