HorizontalAlignment
, Margin
, Padding
, and VerticalAlignment
. The effects of these properties are important to understand, because they provide the basis for controlling the position of elements in Avalonia applications.Panel
element. Fine control of positioning requires an understanding of the HorizontalAlignment
, Margin
, Padding
, and VerticalAlignment
properties.Button
elements in this illustration may appear to be placed randomly. However, their positions are actually precisely controlled by using a combination of margins, alignments, and padding.Border
element encapsulates a parent StackPanel
, with a Padding
value of 15 device independent pixels. This accounts for the narrow LightBlue
band that surrounds the child StackPanel
. Child elements of the StackPanel
are used to illustrate each of the various positioning properties that are detailed in this topic. Three Button
elements are used to demonstrate both the Margin
and HorizontalAlignment
properties.HorizontalAlignment
and VerticalAlignment
properties describe how a child element should be positioned within a parent element's allocated layout space. By using these properties together, you can position child elements precisely. For example, child elements of a DockPanel
can specify four different horizontal alignments: Left
, Right
, Center
, or to Stretch
to fill available space. Similar values are available for vertical positioning.Height
and Width
properties on an element take precedence over the Stretch
property value. Attempting to set Height
, Width
, and a HorizontalAlignment
value of Stretch
results in the Stretch
request being ignored.HorizontalAlignment
property declares the horizontal alignment characteristics to apply to child elements. The following table shows each of the possible values of the HorizontalAlignment
property.Left
Center
Right
Stretch
(Default)Width
and Height
values take precedence.HorizontalAlignment
property to Button
elements. Each attribute value is shown, to better illustrate the various rendering behaviors.HorizontalAlignment
value are visible in the illustration.VerticalAlignment
property describes the vertical alignment characteristics to apply to child elements. The following table shows each of the possible values for the VerticalAlignment
property.Top
Center
Bottom
Stretch
(Default)Width
and Height
values take precedence.VerticalAlignment
property to Button
elements. Each attribute value is shown, to better illustrate the various rendering behaviors. For purposes of this sample, a Grid
element with visible gridlines is used as the parent, to better illustrate the layout behavior of each property value.VerticalAlignment
value are visible in the illustration.Margin
property describes the distance between an element and its child or peers. Margin
values can be uniform, by using syntax like Margin="20"
. With this syntax, a uniform Margin
of 20 device independent pixels would be applied to the element. Margin
values can also take the form of four distinct values, each value describing a distinct margin to apply to the left, top, right, and bottom (in that order), like Margin="0,10,5,25"
. Proper use of the Margin
property enables very fine control of an element's rendering position and the rendering position of its neighbor elements and children.Bounds
.Button
elements. The Button
elements are spaced evenly with a ten-pixel margin buffer in each direction.Margin
in most respects. The Padding property is exposed on only on a few classes, primarily as a convenience: Border
, TemplatedControl
, and TextBlock
are samples of classes that expose a Padding property. The Padding
property enlarges the effective size of a child element by the specified Thickness
value.Padding
to a parent Border
element.HorizontalAlignment
, Margin
, Padding
, and VerticalAlignment
provide the positioning control necessary to create a complex UI. You can use the effects of each property to change child-element positioning, enabling flexibility in creating dynamic applications and user experiences.Grid
element as a child of the Border
in the first sample. Padding
is applied to the parent Border
element. TheGrid
is used to partition space between three child StackPanel
elements. Button
elements are again used to show the various effects of Margin
and HorizontalAlignment
. TextBlock
elements are added to each ColumnDefinition
to better define the various properties applied to the Button
elements in each column.TextBlock
elements.