UIElement.MeasureCore(Size) 方法


在派生类中重写时,提供测量逻辑来适当地调整此元素的大小,兼顾任何子元素内容的大小。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



父元素可以为子元素分配的可用大小。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
  • 必须对每个子元素调用 MeasureYou 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.

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


FrameworkElement 而不是 UIElement派生元素更常见。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. 因此,如果通过不包含 FrameworkElement的继承从 UIElement 派生,则只会重写 MeasureCore 作为更改布局度量值特性的方法。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.