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.

  • 呼叫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而非UIElementIt is more common to derive an element from FrameworkElement rather than UIElement. 如果您衍生自FrameworkElement,請注意,覆寫MeasureCoreFrameworkElement標誌MeasureCore方法。If you are deriving from FrameworkElement, note that an override of MeasureCore on FrameworkElement seals the MeasureCore method. 因此,您只能覆寫MeasureCore做為改變配置量值的特性,如果您衍生自UIElement透過不包含繼承FrameworkElementTherefore, 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.