Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 MeasureOverride Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
FrameworkElement..::.MeasureOverride Method

Updated: November 2007

When overridden in a derived class, measures the size in layout required for child elements and determines a size for the FrameworkElement-derived class.

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

Visual Basic (Declaration)
Protected Overridable Function MeasureOverride ( _
    availableSize As Size _
) As Size
Visual Basic (Usage)
Dim availableSize As Size
Dim returnValue As Size

returnValue = Me.MeasureOverride(availableSize)
C#
protected virtual Size MeasureOverride(
    Size availableSize
)
Visual C++
protected:
virtual Size MeasureOverride(
    Size availableSize
)
J#
protected Size MeasureOverride(
    Size availableSize
)
JScript
protected function MeasureOverride(
    availableSize : Size
) : Size
XAML
You cannot use methods in XAML.

Parameters

availableSize
Type: System.Windows..::.Size

The available size that this element can give to child elements. Infinity can be specified as a value to indicate that the element will size to whatever content is available.

Return Value

Type: System.Windows..::.Size

The size that this element determines it needs during layout, based on its calculations of child element sizes.

Override MeasureOverride to implement custom layout sizing behavior for your element as it participates in the Windows Presentation Foundation (WPF) layout system. Your implementation should do the following:

  1. Iterate your element's particular collection of children that are part of layout, call Measure on each child element.

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

  3. Compute the net desired size of the parent based upon the measurement of the child elements.

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

During this process, child elements might return a larger DesiredSize size than the initial availableSize to indicate that the child element wants more space. This might be handled in your own implementation by introducing a scrollable region, by resizing the parent control, by establishing some manner of stacked order, or any number of solutions for measuring or arranging content.

Important Note:

Elements should call Measure on each child during this process, otherwise the child elements will not be correctly sized or arranged.

Notes to Inheritors:

The following non-compiling code shows this implementation pattern. VisualChildren represents an enumerable collection property of children that your own element should define. The property can be named anything. VisualChildren is a placeholder name for purposes of this example, VisualChildren is not an API as provided by WPF or a part of a naming pattern..

C#
protected override Size MeasureOverride(Size availableSize)
{
    Size desiredSize = new Size();
    foreach (UIElement child in VisualChildren)
    {
        child.Measure(availableSize);
        // do something with child.DesiredSize, either sum them directly or apply whatever logic your element has for reinterpreting the child sizes
        // if greater than availableSize, must decide what to do and which size to return
    }
    // desiredSize = ... computed sum of children's DesiredSize ...;
    // IMPORTANT: do not allow PositiveInfinity to be returned, that will raise an exception in the caller!
    // PositiveInfinity might be an availableSize input; this means that the parent does not care about sizing
    return desiredSize;
}

This example shows how to override the default layout behavior of the Panel element and create custom layout elements that are derived from Panel.

The example defines a simple custom Panel element called PlotPanel, which positions child elements according to two hard-coded x- and y-coordinates. In this example, x and y are both set to 50; therefore, all child elements are positioned at that location on the x and y axes.

To implement custom Panel behaviors, the example uses the MeasureOverride and ArrangeOverride methods. Each method returns the Size data that is necessary to position and render child elements.

Visual Basic
Public Class PlotPanel
    Inherits Panel
    'Override the default Measure method of Panel.

    Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
        Dim childSize As Size = CType(availableSize, Size)
        For Each child As UIElement In InternalChildren
            child.Measure(childSize)
        Next
        Return MyBase.MeasureOverride(availableSize)
    End Function
    Protected Overrides Function ArrangeOverride(ByVal finalSize As System.Windows.Size) As System.Windows.Size
        For Each child As UIElement In InternalChildren
            Dim x As Double = 50
            Dim y As Double = 50
            child.Arrange(New Rect(New System.Windows.Point(x, y), child.DesiredSize))
        Next
        Return MyBase.ArrangeOverride(finalSize)
    End Function
End Class

C#
public class PlotPanel : Panel
{
    // Default public constructor
    public PlotPanel()
        : base()
    {
    }

    // Override the default Measure method of Panel
    protected override Size MeasureOverride(Size availableSize)
    {
        Size panelDesiredSize = new Size();

        // In our example, we just have one child. 
        // Report that our panel requires just the size of its only child.
        foreach (UIElement child in InternalChildren)
        {
            child.Measure(availableSize);
            panelDesiredSize = child.DesiredSize;
        }

        return panelDesiredSize ;
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        foreach (UIElement child in InternalChildren)
        {
            double x = 50;
            double y = 50;

            child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
        }
        return finalSize; // Returns the final Arranged size
    }
}

For the complete sample, see Create a Simple Custom Panel Sample.

Windows Vista

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker