UIElement.MeasureCore(Size) UIElement.MeasureCore(Size) UIElement.MeasureCore(Size) UIElement.MeasureCore(Size) 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.

 virtual System::Windows::Size MeasureCore(System::Windows::Size availableSize);
protected virtual System.Windows.Size MeasureCore (System.Windows.Size availableSize);
abstract member MeasureCore : System.Windows.Size -> System.Windows.Size
override this.MeasureCore : System.Windows.Size -> System.Windows.Size
Protected Overridable Function MeasureCore (availableSize As Size) As Size


Size Size Size Size

父元素可以为子元素分配的可用大小。The available size that the parent element can allocate for the child.


此元素在布局中的所需大小。The desired size of this element in layout.


MeasureCore典型重写遵循此大致模式 (不存在名VisualChildren为的内置集合;VisualChildren表示元素所维护的任何子集合的占位符。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 override Size MeasureCore(Size availableSize)
    foreach (UIElement child in VisualChildren)
        // call some method on child that adjusts child size if needed
    Size desired = CalculateBasedOnCache(_cache);
    return desired;
Protected Overrides Function MeasureCore(ByVal availableSize As Size) As Size
	For Each child As UIElement In VisualChildren
		' call some method on child that adjusts child size if needed
	Next child
	Dim desired As Size = CalculateBasedOnCache(_cache)
	Return desired
End Function
  • 必须对每Measure个子元素调用。You must call Measure on each child element.

  • 通常, 实现应在同一元素中的MeasureCoreArrangeCore方法调用之间缓存度量信息。Generally, your implementation should cache measurement information between the MeasureCore and ArrangeCore method calls in the same element.

  • 不需要调用的MeasureCore基实现, 但如果基实现提供所需的布局功能, 则可能是合适的。Calling base implementations of MeasureCore is not required, but might be appropriate if the base implementation provides a desired layout capability.

  • 对子元素的调用应传递与父元素availableSize相同的, 或传递区域的子集, 具体取决于父元素支持的布局类型。 MeasureCalls 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.


FrameworkElementUIElement而不是派生元素更常见。It is more common to derive an element from FrameworkElement rather than UIElement. 如果是从FrameworkElement派生的, 请注意, FrameworkElement上的MeasureCore重写会MeasureCore密封方法。If you are deriving from FrameworkElement, note that an override of MeasureCore on FrameworkElement seals the MeasureCore method. 因此, 如果UIElement通过不MeasureCore包括FrameworkElement的继承派生, 则只会重写为更改布局度量值特性的方法。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. 如果尝试在核心级别上构建自己的WPFWPF实现, 则可能会出现这种情况。This might be the case if you are attempting to build your own implementation on the WPFWPF core-level. 否则, 如果您是从FrameworkElement派生的, 则度量值行为的实现模板FrameworkElement为的实现。MeasureOverrideOtherwise, if you are deriving from FrameworkElement, then the implementation template for Measure behavior is the FrameworkElement implementation of .MeasureOverride.

具有子元素的父元素必须在Measure每个子元素上调用, 否则这些子元素不会调整大小或排列, 并且将从布局中有效地消失。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.


实现必须能够处理为无限期提供availableSize的值。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.

实现可将提供的availableSize值视为软约束。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.