FrameworkElement.MeasureOverride Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides the behavior for the Measure pass of Silverlight layout. Classes can override this method to define their own Measure pass behavior.

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

Syntax

'Declaration
Protected Overridable Function MeasureOverride ( _
    availableSize As Size _
) As Size
protected virtual Size MeasureOverride(
    Size availableSize
)

Parameters

  • availableSize
    Type: System.Windows.Size
    The available size that this object can give to child objects. Infinity (Double.PositiveInfinity) can be specified as a value to indicate that the object will size to whatever content is available.

Return Value

Type: System.Windows.Size
The size that this object determines it needs during layout, based on its calculations of the allocated sizes for child objects; or based on other considerations, such as a fixed container size.

Remarks

This method has a default implementation that performs built-in layout for most Silverlight FrameworkElement classes.

MeasureOverride effectively provides additional implementation for Measure whenever that method is called either by internal layout logic or your application code. If you are producing a distributable content control, the MeasureOverride logic defines your control's specific Measure pass layout logic.

Notes to Inheritors

For Silverlight (and also for WPF), the technique by which elements are sized and positioned in a layout is divided into two steps: a Measure pass and then an Arrange pass. This technique is explained in more detail in the topic Silverlight Layout System.

Override MeasureOverride to implement custom layout sizing behavior for your class as it participates in the Silverlight layout system. Your implementation should do the following:

  • Iterate your class's particular collection of child objects that are part of layout, call Measure on each child object.

  • Immediately get DesiredSize on each child (this is set as a property after Measure is called).

  • Compute the net desired size of the parent, based on the running measurement of the size that is needed for child objects.

The return value of MeasureOverride should be the object's own desired size, which then becomes the Measure input for the parent of the current object. This same process continues through the layout system until the root of the page/object tree is reached.

During this process, child objects might return a larger DesiredSize size than the initial availableSize to indicate that the child object wants more space. This might be handled in your own implementation by introducing a scrollable region, resizing the parent control, establishing some manner of stacked order, or any number of solutions for measuring or arranging content that can vary depending on your layout container's intended functionality.

Examples

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 Overloads 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
// 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);
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.