Alignment、Margin 和 Padding
最后更新于
最后更新于
An Avalonia control exposes several properties that are used to precisely position child elements. This topic discusses four of the most important properties: 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.
There are numerous ways to position elements using Avalonia. However, achieving ideal layout goes beyond simply choosing the right Panel
element. Fine control of positioning requires an understanding of the HorizontalAlignment
, Margin
, Padding
, and VerticalAlignment
properties.
The following illustration shows a layout scenario that utilizes several positioning properties.
At first glance, the 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.
The following example describes how to create the layout in the preceding illustration. A 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.
The following diagram provides a close-up view of the various positioning properties that are used in the preceding sample. Subsequent sections in this topic describe in greater detail how to use each positioning property.
The 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.
Explicitly set 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.
The 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
Child elements are aligned to the left of the parent element's allocated layout space.
Center
Child elements are aligned to the center of the parent element's allocated layout space.
Right
Child elements are aligned to the right of the parent element's allocated layout space.
Stretch
(Default)
Child elements are stretched to fill the parent element's allocated layout space. Explicit Width
and Height
values take precedence.
The following example shows how to apply the HorizontalAlignment
property to Button
elements. Each attribute value is shown, to better illustrate the various rendering behaviors.
The preceding code yields a layout similar to the following image. The positioning effects of each HorizontalAlignment
value are visible in the illustration.
The 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
Child elements are aligned to the top of the parent element's allocated layout space.
Center
Child elements are aligned to the center of the parent element's allocated layout space.
Bottom
Child elements are aligned to the bottom of the parent element's allocated layout space.
Stretch
(Default)
Child elements are stretched to fill the parent element's allocated layout space. Explicit Width
and Height
values take precedence.
The following example shows how to apply the 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.
The preceding code yields a layout similar to the following image. The positioning effects of each VerticalAlignment
value are visible in the illustration.
The 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.
A non-zero margin applies space outside the element's Bounds
.
The following example shows how to apply uniform margins around a group of Button
elements. The Button
elements are spaced evenly with a ten-pixel margin buffer in each direction.
In many instances, a uniform margin is not appropriate. In these cases, non-uniform spacing can be applied. The following example shows how to apply non-uniform margin spacing to child elements. Margins are described in this order: left, top, right, bottom.
Padding is similar to 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.
The following example shows how to apply 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.
The following example demonstrates each of the concepts that are detailed in this topic. Building on the infrastructure found in the first sample in this topic, this example adds aGrid
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.
When compiled, the preceding application yields a UI that looks like the following illustration. The effects of the various property values are evident in the spacing between elements, and significant property values for elements in each column are shown within TextBlock
elements.