UIElement.Measure(Size) Method

Definition

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.

public:
 virtual void Measure(Size availableSize) = Measure;
void Measure(Size const& availableSize);
public void Measure(Size availableSize);
function measure(availableSize)
Public Sub Measure (availableSize As Size)

Parameters

availableSize
Size

The available space that a parent can allocate to 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.

Examples

This 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);
}
'First measure all children and return available size of panel 
Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size
    'Measure first 9 children giving them space up to 100x100, remaining children get 0x0 
    Dim i As Integer = 0
    For Each child As FrameworkElement In Children
        If i < 9 Then
            child.Measure(New Size(100, 100))
        Else
            child.Measure(New Size(0, 0))
        End If
        i += 1
    Next
    'return the size available to the whole panel, which is 300x300 
    Return New Size(300, 300)
End Function

Remarks

The Measure call potentially reaches a MeasureOverride implementation of that specific class. Otherwise, most FrameworkElement classes have an implicit default layout behavior for Measure.

availableSize can be any number from zero to infinite. Elements participating in layout should return the minimum Size they require for a given availableSize.

Computation of initial layout positioning in a XAML UI consists of a Measure call and an Arrange call, in that order. During the Measure call, the layout system determines an element's size requirements using the availableSize measurement. During the Arrange call, the layout system finalizes the size and position of an element's bounding box.

When a layout is first produced, it always has a Measure call that happens before Arrange. However, after the first layout pass, an Arrange call can happen without a Measure preceding it. This can happen when a property that affects only Arrange is changed (such as alignment), or when the parent receives an Arrange without a Measure.

A Measure call will automatically invalidate any Arrange information. Layout updates generally occur asynchronously (at a time determined by the layout system). An element might not immediately reflect changes to properties that affect element sizing (such as Width).

Applies to

See also