Measure
【UIElement.Measure】的参数时可用空间(Size对象),这个空间通常代表父控件留给子空间显示的可用空间。
/// <summary>
/// Updates DesiredSize of the UIElement. Must be called by parents from theor MeasureCore, to form recursive update.
/// This is first pass of layout update.
/// </summary>
/// <remarks>
/// Measure is called by parents on their children. Internally, Measure calls MeasureCore override on the same object,
/// giving it opportunity to compute its DesiredSize.<para/>
/// This method will return immediately if child is not Dirty, previously measured
/// and availableSize is the same as cached. <para/>
/// This method also resets the IsMeasureinvalid bit on the child.<para/>
/// In case when "unbounded measure to content" is needed, parent can use availableSize
/// as double.PositiveInfinity. Any returned size is OK in this case.
/// </remarks>
/// <param name="availableSize">Available size that parent can give to the child. May be infinity (when parent wants to
/// measure to content). This is soft constraint. Child can return bigger size to indicate that it wants bigger space and hope
/// that parent can throw in scrolling...</param>
public void Measure(Size availableSize)
MeasureOverride
///<Summary>
///<param = "finnalSize">父级中应使用此元素排列自身及其子元素的最终区域</param>
///<return>实际使用的大小</reutnr>
///</Summary>
protected virtual System.Windows.Size ArrangeOverride (System.Windows.Size finalSize);
MeasureCore
接着UIElement内部的MeasureCore会被调用,该方法去决定调用MeasureOverride的大小参数。
具体过程如下:
- 判断大小属性值是否被显示设置。
- 如果是,直接使用设置的值,它总会保持设置的大小(如果超出了父控件的规划大小,WPF会根据IsClipToBounds属性来决定是否裁剪它)
- 如果不是,那么MeasureCore会根据可用大小和当前控件的属性(比如Margin)来决定最终传入MeasureOverride的可用大小参数。
/// <summary>
/// Measurement override. Implement your size-to-content logic here.
/// </summary>
/// <remarks>
/// MeasureCore is designed to be the main customizability point for size control of layout.