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.
ClipToBounds Within a Canvas
Canvas
can position child elements at any position on the screen, even at coordinates that are outside of its own defined Height
and Width
. Furthermore, Canvas
is not affected by the size of its children. As a result, it is possible for a child element to overdraw other elements outside the bounding rectangle of the parent Canvas
. The default behavior of a Canvas
is to allow children to be drawn outside the bounds of the parent Canvas
. If this behavior is undesirable, the ClipToBounds
property can be set to true
. This causes Canvas
to clip to its own size. Canvas
is the only layout element that allows children to be drawn outside its bounds.
Defining and Using a Canvas
A Canvas
can be instantiated simply by using XAML or code. The following example demonstrates how to use Canvas
to absolutely position content. This code produces three 100-pixel squares. The first square is red, and its top-left (x, y) position is specified as (0, 0). The second square is green, and its top-left position is (100, 100), just below and to the right of the first square. The third square is blue, and its top-left position is (50, 50), thus encompassing the lower-right quadrant of the first square and the upper-left quadrant of the second. Because the third square is laid out last, it appears to be on top of the other two squares—that is, the overlapping portions assume the color of the third box.

- XAML
- C#
<Canvas Height="400" Width="400">
<Canvas Height="100" Width="100" Top="0" Left="0" Background="Red"/>
<Canvas Height="100" Width="100" Top="100" Left="100" Background="Green"/>
<Canvas Height="100" Width="100" Top="50" Left="50" Background="Blue"/>
</Canvas>