Build with XuiView Markdown

Stacks and FlexBox

Arrange a row of short labels that wraps as the space narrows. A FlexBox makes the relationship between the items explicit, so you do not need to calculate each item's X coordinate.

using Xui.Core.Canvas;
using Xui.Core.UI;
using Xui.Core.UI.Layout;

public sealed class FlexExample : Border
{
    public FlexExample()
    {
        BackgroundColor = new Color(0xFAF7FFFF);
        Padding = 24;
        var tags = new FlexBox
        {
            FlexWrap = FlexBox.Wrap.Wrap,
            FlexAlignContent = FlexBox.AlignContent.FlexStart,
            FlexAlignItems = FlexBox.AlignItems.FlexStart,
            ColumnGap = 12,
            RowGap = 12
        };
        foreach (var name in new[]
        {
            "Sketch",
            "Build",
            "Explore",
            "Share",
            "Repeat"
        }

        )
        {
            tags.Add(new Border { MinimumWidth = 116, Padding = 16, BackgroundColor = new Color(0xEADCFBFF), Content = new Label { Text = name, FontFamily = "Inter", FontSize = 18 } });
        }

        Content = tags;
    }
}
Stacks and FlexBox: initial rendered view
Stacks and FlexBox · C# source · SVG · Open demo

Use a stack for a simple sequence

VerticalStack places children top to bottom and grows to their combined height. HorizontalStack does the same left to right. Neither wraps. Use child margins for separation, and put a long vertical stack inside a bounded ScrollView.

VerticalUniformStack and HorizontalUniformStack divide constrained space evenly. A uniform stack answers “equal shares”; an ordinary stack answers “each child's desired size.” This distinction matters for button groups and dashboards.

Let FlexBox wrap

The example gives every tag a useful minimum width, adds a gap, and enables FlexBox.Wrap.Wrap. At 400 units wide, fewer tags fit per row than in a large desktop window. The same children remain in the same order.

FlexDirection selects the main axis. FlexJustifyContent distributes spare room along that axis. FlexAlignItems aligns children across it; FlexAlignContent controls lines when wrapping creates several lines. The example uses start alignment to keep a compact cluster at the top.

Gaps belong to the container and only separate items. Padding belongs to the surrounding border and creates space at the edges.

Give one item the spare room

Per-child extras control flexible behavior. For an existing view named editor inside a row FlexBox, this fragment lets it consume remaining width:

editor[Xui.Core.UI.Layout.FlexBox.Grow] = (System.Runtime.InteropServices.NFloat)1;
editor[Xui.Core.UI.Layout.FlexBox.Shrink] = (System.Runtime.InteropServices.NFloat)1;

Basis supplies the starting size on the main axis; its default uses the child's size. Minimum dimensions still matter when shrinking. If you change child extras after the layout has run, invalidate the layout rather than assuming an extra's dictionary assignment schedules another pass.

The current baseline alignment modes fall back to start alignment because views do not yet expose a common layout-baseline contract. Use explicit row alignment when mixing differently sized labels.

Try longer tag names and a narrow viewport. If the content needs strict alignment across both rows and columns, move to Grid layouts.