I have created this grid :
Grid grid = new Grid // new Grid
{
Width = 1500,
Height = 1500,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Background = new SolidColorBrush(Colors.Coral)
};
var columnDefinition = new ColumnDefinition { Width = new GridLength(200) }; // column
var columnDefinition1 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition2 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition3 = new ColumnDefinition { Width = new GridLength(200) };
grid.ColumnDefinitions.Add(columnDefinition); // add column to grid
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
grid.ColumnDefinitions.Add(columnDefinition3);
var rowDefinition = new RowDefinition { Height = new GridLength(300) }; // row
var rowDefinition1 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition2 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition3 = new RowDefinition { Height = new GridLength(300) };
grid.RowDefinitions.Add(rowDefinition); // add row to grid
grid.RowDefinitions.Add(rowDefinition1);
grid.RowDefinitions.Add(rowDefinition2);
grid.RowDefinitions.Add(rowDefinition3);
var textBlock1 = new TextBlock // new textBlock
{
Text = "TextBox 1 ",
FontSize = 20,
Foreground = new SolidColorBrush(Colors.White),
Width = 100
};
Grid.SetColumn(textBlock1, 0);
Grid.SetRow(textBlock1, 0);
var rec = new Rectangle(); // new Rectangle
{
Width = 70;
Height = 70;
rec.Fill = linearGradientBrush;
}
Grid.SetColumn(rec, 1);
Grid.SetRow(rec, 0);
grid.Children.Add(textBlock1);
grid.Children.Add(rec);
MainGrid.Children.Add(grid); // Add grid to main grid
But all I can see is small rectangle of coral color (which is color of my grid) in the middle of the screen. I know it's easier to create grid using XAML, I just want to know how to do it in C#. Thanks for help