Avalonia includes a group of elements that derive from
Panelelements enable many complex layouts. For example, stacking elements can easily be achieved by using the
StackPanelelement, while more complex and free flowing layouts are possible by using a
The following table summarizes the available
Lays out all children to fill the bounds of the
Defines an area within which you can explicitly position child elements by coordinates relative to the Canvas area.
Defines an area within which you can arrange child elements either horizontally or vertically, relative to each other.
Defines a flexible grid area that consists of columns and rows.
Arranges child elements relative to other elements or the panel itself.
Arranges child elements into a single line that can be oriented horizontally or vertically.
Positions child elements in sequential position from left to right, breaking content to the next line at the edge of the containing box. Subsequent ordering occurs sequentially from top to bottom or right to left, depending on the value of the Orientation property.
Panelis an abstract class and laying out multiple controls to fill the available space is usually done with a
Gridwith no rows/columns. In Avalonia
Panelis a usable control that has the same layout behavior as a
Gridwith no rows/columns, but with a lighter runtime footprint.
When thinking about layout in Avalonia, it is important to understand the bounding box that surrounds all elements. Each
Controlconsumed by the layout system can be thought of as a rectangle that is slotted into the layout. The
Boundsproperty returns the boundaries of an element's layout allocation. The size of the rectangle is determined by calculating the available screen space, the size of any constraints, layout-specific properties (such as margin and padding), and the individual behavior of the parent
Panelelement. Processing this data, the layout system is able to calculate the position of all the children of a particular
Panel. It is important to remember that sizing characteristics defined on the parent element, such as a
Border, affect its children.
At its simplest, layout is a recursive system that leads to an element being sized, positioned, and drawn. More specifically, layout describes the process of measuring and arranging the members of a
Childrencollection. Layout is an intensive process. The larger the
Childrencollection, the greater the number of calculations that must be made. Complexity can also be introduced based on the layout behavior defined by the
Panelelement that owns the collection. A relatively simple
Panel, such as
Canvas, can have significantly better performance than a more complex
Panel, such as
Each time that a child control changes its position, it has the potential to trigger a new pass by the layout system. Therefore, it is important to understand the events that can invoke the layout system, as unnecessary invocation can lead to poor application performance. The following describes the process that occurs when the layout system is invoked.
- 1.A child UIElement begins the layout process by first having its core properties measured.
- 2.Sizing properties defined on
Controlare evaluated, such as
Panel-specific logic is applied, such as
Dockdirection or stacking
- 4.Content is arranged after all children have been measured.
Childrencollection is drawn on the screen.
- 6.The process is invoked again if additional
Childrenare added to the collection
This process and how it is invoked are defined in more detail in the following sections.
The layout system completes two passes for each member of the
Childrencollection, a measure pass and an arrange pass. Each child
Panelprovides its own
ArrangeOverridemethods to achieve its own specific layout behavior.
During the measure pass, each member of the
Childrencollection is evaluated. The process begins with a call to the
Measuremethod. This method is called within the implementation of the parent
Panelelement, and does not have to be called explicitly for layout to occur.
First, native size properties of the
IsVisibleare evaluated. This generates a constraint that is passed to
First, framework properties which affects the value of the constraint are processed. These properties generally describe the sizing characteristics of the underlying
Control, such as its
Margin. Each of these properties can change the space that is necessary to display the element.
MeasureOverrideis then called with the constraint as a parameter.
Boundsis a calculated value, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element, and so on.
The ultimate goal of the measure pass is for the child to determine its
DesiredSize, which occurs during the
DesiredSizevalue is stored by
Measurefor use during the content arrange pass.
The arrange pass begins with a call to the
Arrangemethod. During the arrange pass, the parent
Panelelement generates a rectangle that represents the bounds of the child. This value is passed to the
ArrangeCoremethod for processing.
ArrangeCoremethod evaluates the
DesiredSizeof the child and evaluates any additional margins that may affect the rendered size of the element.
ArrangeCoregenerates an arrange size, which is passed to the
ArrangeOverridemethod of the
Panelas a parameter.
ArrangeOverridegenerates the finalSize of the child. Finally, the
ArrangeCoremethod does a final evaluation of offset properties, such as margin and alignment, and puts the child within its layout slot. The child does not have to (and frequently does not) fill the entire allocated space. Control is then returned to the parent
Paneland the layout process is complete.