Layouts

Wisej.NET includes several built-in layout engines and allows developers to easily build custom layout engines. This enables implementing layouts of any complexity, beyond what's available using plain CSS in browsers.

Traditional web frameworks using HTML string concatenation and CSS layouts (Blazor, Angular, PHP, ASP.NET, JSP, etc.) support only a fraction of the layouts available in Wisej.NET applications.

Layout engines handle arranging controls in their container. Every control's LayoutEnginearrow-up-right property returns the current engine and can be overridden in derived classes. The engine measures preferred size for AutoSize controls and arranges position/size of container's direct children.

Layout Engines

Default

All controls use the DefaultLayout engine, which supports:

Docking

Docking applies to child controls in inverse order "away from the viewer". The child control order affects how docking uses available space and intersections between horizontal/vertical docked controls.

Docking demonstration

Controls dock using the container's DisplayRectanglearrow-up-right area, reduced by the Paddingarrow-up-right property.

circle-exclamation

Anchoring

Anchoring styles can be applied to any of the four sides of a control, or none.

When a control has no anchoring (AnchorStyles.Nonearrow-up-right), it will "float" within its container, preserving its relative location. Likewise, if anchoring is not set only for the vertical sides or horizontal sides, the control "floats" vertically or horizontally.

circle-check

The default initial value of the Anchorarrow-up-right property is Top + Left.

Anchor demonstration

Padding and Margins are irrelevant to anchoring.

Flow

The flow layout engine is implemented for the FlowLayoutPanelarrow-up-right. Child controls are arranged horizontally or vertically next to each other.

When using a FlowLayoutPanel in the designer, it extends all its children and adds several extension propertiesarrow-up-right relevant only for flow layout:

  • FillWeight: An arbitrary integer determining whether the child control grows horizontally or vertically (depending on FlowDirectionarrow-up-right) to use remaining space. Default is 0, preserving control size. ⚠️ When using FillWeight, set the control's MinimumSizearrow-up-right to prevent shrinking to 0.

  • FlowBreak: When true, causes a flow break, wrapping to the next line/column depending on the panel's FlowDirectionarrow-up-right.

circle-info

Set these values programmatically using flowLayoutPanel.SetFlowBreak(child, value) or flowLayoutPanel.SetFillWeight(child, value).

In the animation below, green buttons have FillWeight set to 1. Left panel flows horizontally, right panel flows vertically:

Flow layout demonstration
circle-exclamation

Table

The table layout engine is implemented for the TableLayoutPanelarrow-up-right. Child controls are arranged in cells in a grid.

When using the TableLayoutPanel in the designer, it extends its children and adds several extension propertiesarrow-up-right relevant only for table layout:

  • Row, Column, Cell: Determine the grid cell placement for the control. Only one control can occupy a specific cell.

  • RowSpan: Determines how many rows are occupied by the cell.

  • ColumnSpan: Determines how many columns are occupied by the cell.

This layout engine doesn't allow wrapping but supports growing. When adding a child programmatically, you can control whether to add a new row or column when all cells are assigned by setting the GrowStylearrow-up-right property.

Use the RowStylesarrow-up-right and ColumnStylesarrow-up-right collections to determine cell sizing modes. Cells can:

  • Resize proportionally using percentage

  • Auto-size to fit content

  • Have fixed pixel size

Additionally, controls can dock or anchor inside their assigned cell.

The animation below shows a TableLayoutPanel where button3 spans 2 columns and is anchored left and right while vertically centered in the cell. Adding new controls using this code automatically adds new rows when the last row's cells are occupied:

Table layout demonstration
circle-exclamation

Flex

The flex layout engine is implemented for the FlexLayoutPanelarrow-up-right. It comprises two layout engines: HBoxLayout and VBoxLayout. This container arranges its children horizontally or vertically, always filling the client area.

Controls can use Margin, MinimumSizearrow-up-right, MaximumSizearrow-up-right and several extension properties to customize the layout:

  • FillWeight: An arbitrary integer determining whether the child control grows horizontally or vertically (based on LayoutStylearrow-up-right) to use remaining space. Default is 0, preserving control size. ⚠️ When using FillWeight, set the control's MinimumSizearrow-up-right to prevent shrinking to 0.

  • AlignX: Controls horizontal alignment of child controls that can't fill a vertical FlexLayoutPanel due to width constraints. Overrides the default HorizontalAlignarrow-up-right for that control.

  • AlignY: Controls vertical alignment of child controls that can't fill a horizontal FlexLayoutPanel due to height constraints. Overrides the default VerticalAlignarrow-up-right for that control.

The animation below shows two FlexLayoutPanels - first using HBox layout, second using VBox layout. Some buttons have FillWeight set to 1, and button3 is set to align vertically:

Flex layout demonstration
circle-exclamation

Custom

You can build a custom layout engine by deriving from the Wisej.Web.Layout.LayoutEngine class and overriding the Control.LayoutEngine property in your container class.

You can create a single instance (singleton) of your layout engine to reuse, rather than creating a new instance for each container instantiation.

A layout engine needs to implement three methods:

  • InitLayout(child, specifiedBounds): Optional - can use base implementation. Since layout engines can be cached, this call refreshes any internal cache related to a child control.

  • GetPreferredSize(container, proposedSize): Optional - can use base implementation. Used when the container's AutoSize property is enabled and needs to measure children for preferred size.

  • Layout(container, args): Arranges child controls in their container. The simplest layout engine does nothing, letting controls use their own Location and Size.

This sample shows a basic custom layout that arranges child controls in a cascading pattern from top-left to bottom-right:

The CascadeLayout engine works with this CascadingLayoutPanel. It adds a Gap property used by the layout engine:

Depending on the value of the Gap property and the size of the container, this is the result.

Design Time

AutoLayout

Since 3.0

The designer's toolbar has a new option to arrange child controls without setting the Dock or the Anchor properties.

Clicking the AutoLayout button opens the AutoLayout floating panel:

Button
Description

Arranges the controls horizontally, using the available space proportionally.

Arranges the controls vertically, using the available space proportionally.

Docks the controls to the left of the containing area.

Docks the controls to the right of the containing area.

Docks the controls to the bottom of the containing area.

Docks the controls to the top of the containing area.

Resizes the controls to fill the containing area.

Toggles using the controls' margin when applying the auto layout.

Selects the horizontal alignment of the controls within the containing area.

Selects the vertical alignment of the controls within the containing area.

Sets the spacing between the controls in pixels.

Margins

Margins are used by the designer to create proximity snap lines.

When moving a control near another, proximity snap lines appear based on the controls' margins.

The vertical snap line between controls combines the top control's Margin.Bottom and bottom control's Margin.Top values. Using this feature correctly simplifies UI development and helps maintain UI/UX guidelines.

Last updated

Was this helpful?