Panels Overview
Panel
elements are components that control the rendering of elements - their size and dimensions, their position, and the arrangement of their child content. Avalonia UI provides a number of predefined Panel
elements as well as the ability to construct custom Panel
elements.
The Panel Class
Panel
is the base class for all elements that provide layout support in Avalonia. Derived Panel
elements are used to position and arrange elements in XAML and code.
Avalonia includes a comprehensive suite of derived panel implementations that enable many complex layouts. These derived classes expose properties and methods that enable most standard UI scenarios. Developers who are unable to find a child arrangement behavior that meets their needs can create new layouts by overriding the ArrangeOverride
and MeasureOverride
methods. For more information on custom layout behaviors, see Create a Custom Panel.
Panel Common Members
All Panel
elements support the base sizing and positioning properties defined by Control
, including Height
, Width
, HorizontalAlignment
, VerticalAlignment
and Margin
. For additional information on positioning properties defined by Control
, see Alignment, Margins, and Padding Overview.
Panel
exposes additional properties that are of critical importance in understanding and using layout. The Background
property is used to fill the area between the boundaries of a derived panel element with a Brush
. Children
represents the child collection of elements that the Panel
is comprised of.
Attached Properties
Derived panel elements make extensive use of attached properties. An attached property is a specialized form of dependency property that does not have the conventional common language runtime (CLR) property "wrapper". Attached properties have a specialized syntax in XAML, which can be seen in several of the examples that follow.
One purpose of an attached property is to allow child elements to store unique values of a property that is actually defined by a parent element. An application of this functionality is having child elements inform the parent how they wish to be presented in the UI, which is extremely useful for application layout.
User Interface Panels
There are several panel classes available in Avalonia that are optimized to support UI scenarios: Panel
, Canvas
, DockPanel
, Grid
, StackPanel
, WrapPanel
and RelativePanel
. These panel elements are easy to use, versatile, and extensible enough for most applications.
Canvas
The Canvas
element enables positioning of content according to absolute x- and y- coordinates. Elements can be drawn in a unique location; or, if elements occupy the same coordinates, the order in which they appear in markup determines the order in which the elements are drawn.
Canvas
provides the most flexible layout support of any Panel
. Height and Width properties are used to define the area of the canvas, and elements inside are assigned absolute coordinates relative to the area of the parent Canvas
. Four attached properties, Canvas.Left
, Canvas.Top
, Canvas.Right
and Canvas.Bottom
, allow fine control of object placement within a Canvas
, allowing the developer to position and arrange elements precisely on the screen.