Skip to main content
.NET Framework Class Library
UIElementMeasure Method

Updates the DesiredSize of a UIElement. Parent elements call this method from their own MeasureCore implementations to form a recursive layout update. Calling this method constitutes the first pass (the "Measure" pass) of a layout update.

Namespace:   System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax
Public Sub Measure ( _
	availableSize As [%$TOPIC/ms598921_en-us_VS_110_4_0_0_0_0%] _
)
public void Measure(
	[%$TOPIC/ms598921_en-us_VS_110_4_0_1_0_0%] availableSize
)
public:
void Measure(
	[%$TOPIC/ms598921_en-us_VS_110_4_0_2_0_0%] availableSize
)
member Measure : 
        availableSize:[%$TOPIC/ms598921_en-us_VS_110_4_0_3_0_0%] -> unit

Parameters

availableSize
Type: System.WindowsSize

The available space that a parent element can allocate a child element. A child element can request a larger space than what is available; the provided size might be accommodated if scrolling is possible in the content model for the current element.

Remarks

Computation of layout positioning in Windows Presentation Foundation (WPF) is comprised of a Measure call and an Arrange call. During the Measure call, an element determines its size requirements by using an availableSize input. During the Arrange call, the element size is finalized.

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

When a layout is first instantiated, it always receives a Measure call before Arrange. However, after the first layout pass, it may receive an Arrange call without a Measure; 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 an Arrange call.

Layout updates happen asynchronously, such that the main thread is not waiting for every possible layout change. Querying an element via code-behind checking of property values may not immediately reflect changes to properties that interact with the sizing or layout characteristics (the Width property, for example).

NoteNote

Layout updates can be forced by using the UpdateLayout method. However, calling this method is usually unnecessary and can cause poor performance.

The layout system keeps two separate queues of invalid layouts, one for Measure and one for Arrange. The layout queue is sorted based upon the order of elements in the visual tree of the element performing layout; elements higher in the tree are at the top of the queue, to avoid redundant layouts caused by repeated changes in parents. Duplicate entries are automatically removed from the queue, and elements are automatically removed from the queue if they are already layout-validated.

When updating layout, the Measure queue is emptied first, followed by the Arrange queue. An element in the Arrange queue will never be arranged if there is an element in the Measure queue.

Examples

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.

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 panelDesiredSize As Size = New Size()
        ' In our example, we just have one child.  
        ' Report that our panel requires just the size of its only child. 
        For Each child As UIElement In InternalChildren
            child.Measure(availableSize)
            panelDesiredSize = child.DesiredSize
        Next 
        Return panelDesiredSize
    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 finalSize
    End Function 
End Class
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
    }
}
public: 
      ref class PlotPanel : Panel {

      public: 
         PlotPanel () {};

      protected: 
         // Override the default Measure method of Panel 
         virtual Size MeasureOverride(Size availableSize) override
         {
             Size^ panelDesiredSize = gcnew Size();

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

      protected: 
         virtual System::Windows::Size ArrangeOverride (Size finalSize) override 
         {
            for each (UIElement^ child in InternalChildren)
            {
               double x = 50;
               double y = 50;
               child->Arrange(Rect(Point(x, y), child->DesiredSize));
            }
            return finalSize;
         };
      };
Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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