UIElement.DesiredSize プロパティ

定義

レイアウト プロセスのメジャー パス中にこの UIElement が計算したサイズを取得します。

public:
 property Size DesiredSize { Size get(); };
Size DesiredSize();
public Size DesiredSize { get; }
var size = uIElement.desiredSize;
Public ReadOnly Property DesiredSize As Size

プロパティ値

レイアウト プロセスのメジャー パス中にこの UIElement が計算したサイズ。

次の使用例は、 ArrangeOverride 実装の子イテレーションの一部として DesiredSize に対してクエリを実行します。

// Second arrange all children and return final size of panel
protected override Size ArrangeOverride(Size finalSize)
{
    // Get the collection of children
    UIElementCollection mychildren = Children;

    // Get total number of children
    int count = mychildren.Count;

    // Arrange children
    // We're only allowing 9 children in this panel.  More children will get a 0x0 layout slot.
    int i;
    for (i = 0; i < 9; i++)
    {

        // Get (left, top) origin point for the element in the 3x3 block
        Point cellOrigin = GetOrigin(i, 3, new Size(100, 100));

        // Arrange child
        // Get desired height and width. This will not be larger than 100x100 as set in MeasureOverride.
        double dw = mychildren[i].DesiredSize.Width;
        double dh = mychildren[i].DesiredSize.Height;

        mychildren[i].Arrange(new Rect(cellOrigin.X, cellOrigin.Y, dw, dh));

    }

    // Give the remaining children a 0x0 layout slot
    for (i = 9; i < count; i++)
    {
        mychildren[i].Arrange(new Rect(0, 0, 0, 0));
    }


    // Return final size of the panel
    return new Size(300, 300);
}

注釈

通常、DesiredSize は、 ArrangeOverride や MeasureOverride などのレイアウト動作のオーバーライドを実装するときに、測定要因の 1 つとしてチェック されます。 親コンテナーのレイアウト ロジックによっては、DesiredSize が完全に尊重され、DesiredSize に対する制約が適用される場合があります。また、このような制約によって、親要素または子要素の他の特性も変更される可能性があります。 たとえば、スクロール可能な領域をサポートするコントロール (ただし、スクロール可能な領域を既に有効にしているコントロールから派生しないことを選択) では、使用可能なサイズを DesiredSize と比較できます。 その後、コントロールは、そのコントロールの UI でスクロール バーを有効にする内部状態を設定できます。 または、DesiredSize を無視して、添付プロパティ値の確認などの他の考慮事項によってサイズが変更されたレイアウトを要素が常に取得できます。

DesiredSize には、要素に対して少なくとも 1 つの "Measure" パスのレイアウトが実行されていない限り、有用な値は含まれません。

DesiredSize は実際には、独自のレイアウト オーバーライド メソッドを定義する場合にのみ使用するためのものです。 実行時にアプリの UI 内の要素のサイズに関心がある場合は、代わりに ActualWidth プロパティと ActualHeight プロパティを使用する必要があります。 グリッド セルのサイズ設定などの動的レイアウト手法によって要素が影響を受ける場合は、この方法でサイズstar確認している可能性があります。 ActualWidthActualHeight の値は、レイアウトの実行後に確実に実行される状況 (Loaded イベントなど)、または UI が最初にレンダリングされた後にのみ可能なユーザー アクションによってトリガーされる場合にのみ使用します。

適用対象

こちらもご覧ください