Windows Presentation Foundation Series (4) -- common layout attributes

Source: Internet
Author: User

Disclaimer: Anyone and organizations are welcome to repost the articles in this blog, but the original links and author information must be marked.

Link: http://www.cnblogs.com/leezhm/archive/2011/07/01/2095649.html

Pioneering little turtle -------> csdn

In the WPF layout, apart from the different additional attributes used by different panels, there are also some common layout attributes. They make the layout system more powerful and more precise. They are

Attribute Description
Width Specify a width for the element.
Height Specify a height for the element.
Minwidth The minimum allowed element width.
Minheight The minimum allowed element height.
Maxwidth The maximum width of the element.
Maxheight Maximum allowed element height.
Horizontalalignment Controls the horizontal position of an element when the element is smaller than the available space.
Verticalalignment Controls the vertical position of an element when the element is smaller than the available space.
Margin Set the spacing between elements and the surrounding border.
Padding Set the spacing between the element border and the element content.
Visibility Sets whether the element is visible in the layout system.
Flowdirection Set the text direction.
Panel. zindex Set the Z axis of the element to control the actual sequence of the element.
Rendertransform Set a conversion for the element without affecting the layout system.
Layouttransform Setting a conversion for the element will affect the layout system and trigger the system to perform the layout again.
All the attributes in the preceding table, except padding and panel. zindex, are the attributes of the frameworkelement class. Most WPF controls are integrated with frameworkelement. (1) width and height can be used to set the exact fixed size of a control through width and height, but we should avoid using them in practical applications, you should also allow the control to determine its own size. By allowing the control to adjust the size based on the content, you can easily change the interface and adjust the layout. Because if you set a fixed size, the layout always tries to follow your choice in every adjustment to meet the fixed size and perform multiple operations. (2) minwidth, minheight, maxwidth, and maxheight are clearly set to the lower limit and upper limit of the control size. When you need to limit the layout of the Panel, it provides you with the best and most appropriate method. But it is also valid within a reasonable range. For example, if you set minwidth to 10000, this obviously cannot be implemented, and WPF cannot meet your requirements. (3) horizontalalignment and verticalignment when the available space in the parent element has found the space required by the child element, the layout will involve the filling of the remaining space. In this case, you can use these attributes to control the position of child elements. For example, a stackpanel with an orientation set to vertical is usually as wide as the widest child element, which means that other narrow elements and panel boundary will have additional space, in this case, alignment must be used to determine how the elements process the remaining space. The default values of these two attributes are stretch, that is, when there is extra space, the elements are enlarged to fill the space. Horizontalalignment can also be set to center, left, right, and verticalignment can be set to center, bottom, and top. Compare the content of the following images to understand the above.

When the control sets maxwidth and maxheight respectively, and the Panel has additional available space, the control layout displays the value of maxwidth and maxheight. When the Panel has no extra space but is larger than the minwidth and minheight set for the space, the control will layout the available space. When the layout size is smaller than the minwidth and minheight settings of the control, the control does not change the size, but the Panel such as uniformgrid will be cut

 

After horizontalalignment and verticalignment of the control are set, the Panel will layout the control with minwidth and minheight respectively. Therefore, when both the horizontal and vertical directions are set, the control will be laid out in the smallest size. When the control sets both the fixed values of width and height, the panel displays the control with a fixed value.

(4) margin

The purpose of margin is to adjust the space around the elements and the spacing between the elements and the edge of the Panel (not completely accurate, but more accurate is the spacing between the elements and the edge of the surrounding elements ). For details about how to use it, refer to the msdn:

(5) padding

Padding specifies the spacing between the element border and the internal content. Not all WPF elements have internal content, so not all WPF elements have the padding attribute. For details, see the msdn explanation:

(6) Visibility & flowdirection & panel. zindex

Visibility determines whether the element is visible. The value of visibility is determined by enumeration system. Windows. Visibility. Because different enumeration values affect the layout, exercise caution. The following table shows the specific values:

Member name

Description

Visible

Display Element

Hidden

Do not show elements, but reserve layout space for elements

Collapsed

Do not display elements, and do not reserve layout space for elements

Flowdirection can control the direction of the text (Note: although it is mainly used to control the direction of the text, it also works on the wrappanel panel, which affects the arrangement of elements in wrappanel, by the way, left to right, or right-to-left), which is based on the local settings of the system by default. Panel. zindex is an additional attribute that determines the display sequence of two overlapping elements. By default, the zindex of elements is determined by the order they define, but the display order of elements can be determined by explicitly specifying the zindex value. (7) In WPF, rendertransform & layouttransform can use rendertransform and layouttransform to scale and rotate elements. Although the effects are the same, they are different. Changes caused by layouttransform transformation will be included in the calculation scope of the layout system, while changes caused by rendertransform transformation will be ignored. Obviously, every layouttransform transformation will trigger a layout on the Panel. In some unnecessary situations, it will definitely consume a lot of system resources. Therefore, you must disable layouttransform when the requirements are met. The following two simple examples are used for understanding:

Obviously, through the comparison above, layouttransform causes a change in the panel layout, while the rendertransform variation rule is ignored.

 

Summary: The previous section briefly introduced several common layout attributes. The specific application also needs to be summarized in practice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.