FrameworkElement.MeasureOverride(Size) Method


在派生类中重写时,测量子元素在布局中所需的大小,并确定由 FrameworkElement 派生的类的大小。When overridden in a derived class, measures the size in layout required for child elements and determines a size for the FrameworkElement-derived class.

 virtual System::Windows::Size MeasureOverride(System::Windows::Size availableSize);
protected virtual System.Windows.Size MeasureOverride (System.Windows.Size availableSize);
abstract member MeasureOverride : System.Windows.Size -> System.Windows.Size
override this.MeasureOverride : System.Windows.Size -> System.Windows.Size
Protected Overridable Function MeasureOverride (availableSize As Size) As 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.



此元素基于其对子元素大小的计算确定它在布局期间所需要的大小。The size that this element determines it needs during layout, based on its calculations of child element sizes.


重写 MeasureOverride 以实现元素在 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 布局系统中的自定义布局大小调整行为。Override MeasureOverride to implement custom layout sizing behavior for your element as it participates in the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) layout system. 你的实现应执行以下操作:Your implementation should do the following:

  1. 循环访问作为布局一部分的元素的特定子集合,对每个子元素调用 MeasureIterate your element's particular collection of children that are part of layout, call Measure on each child element.

  2. 立即获取子级的 DesiredSize (在调用 Measure 后,此设置为属性)。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.

MeasureOverride 的返回值应为元素自己所需的大小,该大小随后将成为当前元素的父元素的度量输入。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.

在此过程中,子元素返回的 DesiredSize 大小可能大于初始 availableSize,以指示子元素需要更多空间。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.


在此过程中,元素应在每个子元素上调用 Measure,否则,将不会正确调整或排列子元素的大小。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表示你自己的元素应定义的子元素的可枚举集合属性。VisualChildren represents an enumerable collection property of children that your own element should define. 属性可以命名为任意名称。The property can be named anything. VisualChildren是占位符名称。在此示例中, VisualChildren不是 WPFWPF 或命名模式的一部分提供的 API。VisualChildren is a placeholder name for purposes of this example, VisualChildren is not an API as provided by WPFWPF or a part of a naming pattern.

[! code-csharpCorePseudocode # FEMeasureOverride][! code-vbCorePseudocode # FEMeasureOverride][!code-csharpCorePseudocode#FEMeasureOverride] [!code-vbCorePseudocode#FEMeasureOverride]

Applies to