UIElement.Measure Method

July 28, 2014

Updates the DesiredSize of a UIElement. Typically, objects that implement custom layout for their layout children call this method from their own MeasureOverride implementations to form a recursive layout update.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

public void Measure(
	Size availableSize
)

Parameters

availableSize
Type: System.Windows.Size
The available space that a parent can allocate a child object. A child object can request a larger space than what is available; the provided size might be accommodated if scrolling or other resize behavior is possible in that particular container.

ExceptionCondition
InvalidOperationException

availableSize contained a Double.NaN value. See Remarks.

There is no reason to call Measure or Arrange outside of the context of overriding methods that perform custom layout actions. Windows Phone layout works autonomously, based on detecting changes to the object tree and layout-relevant properties at run time. For more information, see Layout for Windows Phone 8.

The availableSize you pass to Measure cannot have a Double.NaN value for either the Height or Width of the Size. availableSize values can be any number from zero to infinite. Elements participating in layout should return the minimum Size they require for a given availableSize.

The following example implements MeasureOverride to customize the Measure pass logic for a custom panel implementation. Note in particular these aspects of the code:

  • Iterates over children.

  • For each child, calls Measure, using a Size that makes sense based on how the panel logic treats the number of children and its own known size limit.

  • Returns its size (in this case, this simple panel returns a fixed size rather than a size calculated on accumulating the measurements).


// First measure all children and return available size of panel
protected override Size MeasureOverride(Size availableSize)
{

    // Measure first 9 children giving them space up to 100x100, remaining children get 0x0 
    int i =0;
    foreach (FrameworkElement child in Children)
    {
        if (i < 9)
        {
            child.Measure(new Size(100, 100));
        }
        else
        {
            child.Measure(new Size(0, 0));
        }

        i++;
    }


    // return the size available to the whole panel, which is 300x300
    return new Size(300,300);
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft