面板概述
Panel
elements are components that control the rendering of elements - their size and dimensions, their position, and the arrangement of their child content. Avalonia 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.
C#
XAML
DockPanel
The DockPanel
element uses the DockPanel.Dock
attached property as set in child content elements to position content along the edges of a container. When DockPanel.Dock
is set to Top
or Bottom
, it positions child elements above or below each other. When DockPanel.Dock
is set to Left
or Right
, it positions child elements to the left or right of each other. The LastChildFill
property determines the position of the final element added as a child of a DockPanel
.
You can use DockPanel
to position a group of related controls, such as a set of buttons. Alternately, you can use it to create a "paned" UI.
Sizing to Content
If its Height
and Width
properties are not specified, DockPanel
sizes to its content. The size can increase or decrease to accommodate the size of its child elements. However, when these properties are specified and there is no longer room for the next specified child element, DockPanel
does not display that child element or subsequent child elements and does not measure subsequent child elements.
LastChildFill
By default, the last child of a DockPanel
element will "fill" the remaining, unallocated space. If this behavior is not desired, set the LastChildFill
property to false
.
Defining and Using a DockPanel
The following example demonstrates how to partition space using a DockPanel
. Five Border
elements are added as children of a parent DockPanel
. Each uses a different positioning property of a DockPanel
to partition space. The final element "fills" the remaining, unallocated space.
C#
XAML
Grid
The Grid
element merges the functionality of an absolute positioning and tabular data control. A Grid
enables you to easily position and style elements. Grid
allows you to define flexible row and column groupings, and even provides a mechanism to share sizing information between multiple Grid
elements.
Sizing Behavior of Columns and Rows
Columns and rows defined within a Grid
can take advantage of Star
sizing in order to distribute remaining space proportionally. When Star
is selected as the Height or Width of a row or column, that column or row receives a weighted proportion of remaining available space. This is in contrast to Auto
, which will distribute space evenly based on the size of the content within a column or row. This value is expressed as *
or 2*
when using XAML. In the first case, the row or column would receive one times the available space, in the second case, two times, and so on. By combining this technique to proportionally distribute space with a HorizontalAlignment
and VerticalAlignment
value of Stretch
it is possible to partition layout space by percentage of screen space. Grid
is the only layout panel that can distribute space in this manner.
Defining and Using a Grid
The following example demonstrates how to build a UI similar to that found on the Run dialog available on the Windows Start menu.
C#
StackPanel
A StackPanel
enables you to "stack" elements in an assigned direction. The default stack direction is vertical. The Orientation
property can be used to control content flow.
StackPanel vs. DockPanel
Although DockPanel
can also "stack" child elements, DockPanel
and StackPanel
do not produce analogous results in some usage scenarios. For example, the order of child elements can affect their size in a DockPanel
but not in a StackPanel
. This is because StackPanel
measures in the direction of stacking at PositiveInfinity
, whereas DockPanel
measures only the available size.
Defining and Using a StackPanel
The following example demonstrates how to use a StackPanel
to create a set of vertically-positioned buttons. For horizontal positioning, set the Orientation
property to Horizontal
.
C#
WrapPanel
WrapPanel
is used to position child elements in sequential position from left to right, breaking content to the next line when it reaches the edge of its parent container. Content can be oriented horizontally or vertically. WrapPanel
is useful for simple flowing UI scenarios. It can also be used to apply uniform sizing to all of its child elements.
The following example demonstrates how to create a WrapPanel
to display Button
controls that wrap when they reach the edge of their container.
C#
XAML
Nested Panel Elements
Panel
elements can be nested within each other in order to produce complex layouts. This can prove very useful in situations where one Panel
is ideal for a portion of a UI, but may not meet the needs of a different portion of the UI.
There is no practical limit to the amount of nesting that your application can support, however, it is generally best to limit your application to only use those panels that are actually necessary for your desired layout. In many cases, a Grid
element can be used instead of nested panels due to its flexibility as a layout container. This can increase performance in your application by keeping unnecessary elements out of the tree.
最后更新于