Grid
Introduction
Code Example: Creating a Grid
// Initialize
var grid = new Grid();
// Grid fills its parent by default
grid.AddToRoot();
// Three columns showing the three main sizing types
grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(1, GridUnitType.Star)));
grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(2, GridUnitType.Star)));
grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Auto));
// Two rows with different proportions
grid.RowDefinitions.Add(new RowDefinition(new GridLength(2, GridUnitType.Star)));
grid.RowDefinitions.Add(new RowDefinition(new GridLength(1, GridUnitType.Star)));
// Colors for each column — makes proportions immediately visible
Color[] columnColors = [
new Color(180, 100, 100), // red-ish
new Color(100, 150, 180), // blue-ish
new Color(100, 170, 120), // green-ish
];
string[] columnLabels = ["1*", "2*", "Auto"];
string[] rowLabels = ["2*", "1*"];
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 3; col++)
{
var background = new ColoredRectangleRuntime();
float brightness = row == 0 ? 1.0f : 0.6f;
var cellColor = new Color(
(int)(columnColors[col].R * brightness),
(int)(columnColors[col].G * brightness),
(int)(columnColors[col].B * brightness)
);
background.Color = cellColor;
background.Dock(Dock.Fill);
grid.AddChild(background, row: row, column: col);
var label = new Label();
label.Text = $"Col {columnLabels[col]}\nRow {rowLabels[row]}";
label.XUnits = GeneralUnitType.PixelsFromMiddle;
label.YUnits = GeneralUnitType.PixelsFromMiddle;
label.X = 0;
label.Y = 0;
label.XOrigin = HorizontalAlignment.Center;
label.YOrigin = VerticalAlignment.Center;
grid.AddChild(label, row: row, column: col);
}
}
Row and Column Sizing
Star (Proportional)
Absolute
Auto
Adding Children
Removing Children
Supported Ways to Add Children
Constraining Column and Row Sizes
Last updated
Was this helpful?

