The process that WPF determine the layout of UIelements

 

(Extract from <<Application = Code + Markup>>)

The argument to MeasureOverride (which is commonly called sizeAvailable) is generally the size of the element's container minus the element's Margin property. This argument can range from 0 to positive infinity. It is infinite if the container can size itself to the size of its children. However, if the element's Height property has been set to something other than NaN, sizeAvailable.Height will equal that property. If MinHeight or MaxHeight has been set, sizeAvailable.Height will reflect those constraints, and similarly for width.

MeasureOverride is responsible for calling Measure for each of its children. If an element has one child and the child is to occupy the entire surface of the element, the argument to Measure can be the same sizeAvailable parameter to MeasureOverride. Most commonly, the argument to Measure is a size based on sizeAvailable but somewhat less than that size. For example, EllipseWithChild offers its child a size from which the ellipse's own border has been subtracted.

The Measure method in the child calls the child's MeasureOverride method and uses the return value of MeasureOverride to calculate its own DesiredSize property. This calculation can be conceived as occurring in several steps. Here's how DesiredSize.Width is calculated. (The other dimension is calculated identically.)

1.      If the element's Width property is NaN, DesiredSize.Width is the return value from MeasureOverride. Otherwise, DesiredSize.Width equals the element's Width.

2.      DesiredSize.Width is adjusted by adding the Left and Right properties of the Margin.

3.      DesiredSize.Width is adjusted so that it is not larger than the width of the container.

So, DesiredSize is basically the layout size required of the element including the element's margin, but not larger than the container. It is not very useful for an element to examine its own DesiredSize, but the DesiredSize properties of the element's children are very useful. After calling Measure on each of its children, an element can examine the children's DesiredSize properties to determine how much space each child needs and then calculate its own return value from the MeasureOverride method.

The parameter to ArrangeOverride (commonly called sizeFinal) is calculated from the original parameter to MeasureOverride and the return value from MeasureOverride, with a couple of other factors. Here's how sizeFinal.Width is calculated:

(Warning: the text below is wrong)

If Width, MinWidth, and MaxWidth are all NaN and the HorizontalAlignment property of the element is set to Center, Left, or Right, sizeFinal.Width equals the return value from MeasureOverride. If HorizontalAlignment is set to Stretch, sizeFinal.Width equals the maximum of the sizeAvailable argument to MeasureOverride and the return value from MeasureOverride. If the Width property has been set to something other than NaN, sizeFinal.Width is the maximum of the Width property and the return value from MeasureOverride.

(The correct text from MSND)

The arrange process begins with a call to the Arrange method. During the arrange pass, the parent Panel element generates a rectangle that represents the bounds of the child. This value is passed to the ArrangeCore method for processing.

The ArrangeCore method evaluates the DesiredSize of the child and evaluates any additional margins that may affect the rendered size of the element and generates an arrangeSize, which is passed on to the ArrangeOverride of the Panel(But I think there is child) as a parameter. ArrangeOverride generates the finalSize of the child, and finally the ArrangeCore method does a final evaluation of offset properties such as margin and alignment and places the child within its layout slot. The child does not need to (and frequently won't) fill the entire allocated space. Control is then returned to the parent Panel, and the layout process is complete.

After calling Arrange on its children, ArrangeOverride generally returns its sizeFinal parameter. (The base implementation of ArrangeOverride does precisely that.) However, it could return something different. Whatever ArrangeOverride returns becomes RenderSize.

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章