Export (0) Print
Expand All
Expand Minimize

UIElement.MeasureCore Method

When overridden in a derived class, provides measurement logic for sizing this element properly, with consideration of the size of any child element content.

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

'Declaration
Protected Overridable Function MeasureCore ( _
	availableSize As Size _
) As Size

Parameters

availableSize
Type: System.Windows.Size

The available size that the parent element can allocate for the child.

Return Value

Type: System.Windows.Size
The desired size of this element in layout.

It is more common to derive an element from FrameworkElement rather than UIElement. If you are deriving from FrameworkElement, note that an override of MeasureCore on FrameworkElement seals the MeasureCore method. Therefore, you only override MeasureCore as a means to alter layout measure characteristics if you derive from UIElement through an inheritance that does not include FrameworkElement. This might be the case if you are attempting to build your own implementation on the WPF core-level. Otherwise, if you are deriving from FrameworkElement, then the implementation template for Measure behavior is the FrameworkElement implementation of .MeasureOverride.

A parent element with child elements must call Measure on each child, otherwise these child elements are not sized or arranged and will effectively disappear from layout.

Notes to Inheritors

Implementations must be able to process a value provided for availableSize that is infinite. An infinite value indicates no requested constraints, and effectively defers measurement choice to the parent element, through recursive Measure calls.

Implementations can consider the value provided for availableSize to be a soft constraint. The child element might specify a larger size, even if other aspects of application code were able to determine the current actual size of the parent element. The large size request is a convention that indicates that the child element is querying whether your parent element can support content scrolling within a content display region.

A typical override of MeasureCore follows this approximate pattern (there is not a built-in collection called VisualChildren; VisualChildren is a placeholder that represents whatever child collection your element maintains).

Protected Overrides Function MeasureCore(ByVal availableSize As Size) As Size
	For Each child As UIElement In VisualChildren
		child.Measure(availableSize)
		' call some method on child that adjusts child size if needed
		_cache.StoreInfoAboutChild(child)
	Next child
	Dim desired As Size = CalculateBasedOnCache(_cache)
	Return desired
End Function
  • You must call Measure on each child element.

  • Generally, your implementation should cache measurement information between the MeasureCore and ArrangeCore method calls in the same element.

  • Calling base implementations of MeasureCore is not required, but might be appropriate if the base implementation provides a desired layout capability.

  • Calls to Measure on child elements should pass either the same availableSize as the parent, or a subset of the area, depending on the type of layout the parent element supports. For example, it would be valid to remove the area for an element-specific border or padding, a scrollbar, or a custom control.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft