言語

FrameworkElement.MeasureOverride(Size) メソッド

定義

派生クラスでオーバーライドされると、子要素に必要なレイアウトのサイズを測定し、 FrameworkElement派生クラスのサイズを決定します。

protected:
 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

パラメーター

availableSize
Size

この要素が子要素に与えることができる使用可能なサイズ。 無限大は、要素が使用可能なコンテンツに合ったサイズにすることを示す値として指定できます。

返品

子要素のサイズの計算に基づいて、この要素がレイアウト中に必要と判断するサイズ。

注釈

Windows Presentation Foundation (WPF) レイアウト システムに参加する要素のカスタム レイアウト サイズ設定動作を実装するには、MeasureOverride をオーバーライドします。 実装では、次の操作を行う必要があります。

  1. レイアウトの一部である要素の子の特定のコレクションを反復処理し、各子要素に対して Measure を呼び出します。

  2. すぐに子に DesiredSize を取得します (これは、 Measure が呼び出された後にプロパティとして設定されます)。

  3. 子要素の測定に基づいて、親のネット希望サイズを計算します。

MeasureOverrideの戻り値は、要素独自の目的のサイズである必要があります。これは、現在の要素の親要素のメジャー入力になります。 この同じプロセスは、ページのルート要素に到達するまで、レイアウト システムを通じて続行されます。

このプロセス中に、子要素が最初のDesiredSizeよりも大きなavailableSize サイズを返して、子要素がより多くの領域を必要としていることを示す場合があります。 これは、スクロール可能な領域の導入、親コントロールのサイズ変更、何らかの積み上げ順序の確立、またはコンテンツの測定または配置のための任意の数のソリューションによって、独自の実装で処理される場合があります。

Important

要素は、このプロセス中に各子に対して Measure を呼び出す必要があります。それ以外の場合、子要素のサイズが正しくないか、配置されません。

注意 (継承者)

次の非コンパイル コードは、この実装パターンを示しています。 VisualChildren は、独自の要素で定義する必要がある子の列挙可能なコレクション プロパティを表します。 プロパティには任意の名前を付けることができます。 VisualChildren は、この例の目的でプレースホルダー名です。VisualChildren は、WPFまたは名前付けパターンの一部によって提供される API ではありません。

protected override Size MeasureOverride(Size availableSize)
{
    Size desiredSize = new Size();
    foreach (UIElement child in VisualChildren)
    {
        child.Measure(availableSize);
        // do something with child.DesiredSize, either sum them directly or apply whatever logic your element has for reinterpreting the child sizes
        // if greater than availableSize, must decide what to do and which size to return
    }
    // desiredSize = ... computed sum of children's DesiredSize ...;
    // IMPORTANT: do not allow PositiveInfinity to be returned, that will raise an exception in the caller!
    // PositiveInfinity might be an availableSize input; this means that the parent does not care about sizing
    return desiredSize;
}
Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size
    Dim desiredSize As New Size()
    For Each child As UIElement In VisualChildren
        child.Measure(availableSize)
        ' do something with child.DesiredSize, either sum them directly or apply whatever logic your element has for reinterpreting the child sizes
        ' if greater than availableSize, must decide what to do and which size to return
    Next child
    ' desiredSize = ... computed sum of children's DesiredSize ...
    ' IMPORTANT: do not allow PositiveInfinity to be returned, that will raise an exception in the caller!
    ' PositiveInfinity might be an availableSize input - this means that the parent does not care about sizing
    Return desiredSize
End Function

適用対象