UIElement.DesiredSize UIElement.DesiredSize UIElement.DesiredSize UIElement.DesiredSize Property

定義

取得這個項目在版面配置處理序的測量傳遞期間所計算的大小。Gets the size that this element computed during the measure pass of the layout process.

public:
 property System::Windows::Size DesiredSize { System::Windows::Size get(); };
public System.Windows.Size DesiredSize { get; }
member this.DesiredSize : System.Windows.Size
Public ReadOnly Property DesiredSize As Size

屬性值

成為排列傳遞所需大小的計算大小。The computed size, which becomes the desired size for the arrange pass.

範例

下列範例所示DesiredSize一部分MeasureOverride實作。The following example shows DesiredSize as part of a MeasureOverride implementation. 請注意如何Measure呼叫之前取得DesiredSizeNotice how Measure is called immediately prior to obtaining DesiredSize. 這可確保DesiredSize保留的合法值。This assures that DesiredSize holds a legitimate value.

    virtual Size MeasureOverride(Size availableSize) override
    {
        Size^ panelDesiredSize = gcnew Size();

        // In our example, we just have one child. 
        // Report that our panel requires just the size of its only child.
        for each (UIElement^ child in InternalChildren)
        {
            child->Measure(availableSize);
panelDesiredSize = child->DesiredSize;
        }
        return *panelDesiredSize ;
    }
protected override Size MeasureOverride(Size availableSize)
{
    Size panelDesiredSize = new Size();

    // In our example, we just have one child. 
    // Report that our panel requires just the size of its only child.
    foreach (UIElement child in InternalChildren)
    {
        child.Measure(availableSize);
        panelDesiredSize = child.DesiredSize;
    }

    return panelDesiredSize ;
}
Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
    Dim panelDesiredSize As Size = New Size()
    ' In our example, we just have one child. 
    ' Report that our panel requires just the size of its only child.
    For Each child As UIElement In InternalChildren
        child.Measure(availableSize)
        panelDesiredSize = child.DesiredSize
    Next
    Return panelDesiredSize
End Function

備註

這個屬性所傳回的值才會有效度量如果的值IsMeasureValid屬性是trueThe value returned by this property will only be a valid measurement if the value of the IsMeasureValid property is true.

DesiredSize 當其中一個度量因素,當您實作版面配置行為覆寫這類,通常會檢查ArrangeOverrideMeasureOverride,或OnRender(在OnRender情況下,您可以檢查RenderSize相反的但這取決於您實作)。DesiredSize is typically checked as one of the measurement factors when you implement layout behavior overrides such as ArrangeOverride, MeasureOverride, or OnRender (in the OnRender case, you might check RenderSize instead, but this depends on your implementation). 根據案例中,DesiredSize可能會完全遵守您的實作邏輯,條件約束上DesiredSize可能會套用,以及這類條件約束也可能會變更其他特性的父項目或子項目。Depending on the scenario, DesiredSize might be fully respected by your implementation logic, constraints on DesiredSize might be applied, and such constraints might also change other characteristics of either the parent element or child element. 例如,支援可捲動區域 (卻選擇不是衍生自已經啟用 可捲動區域的 WPF 架構層級控制項) 的控制項無法比較可用的大小,以DesiredSizeFor example, a control that supports scrollable regions (but chooses not to derive from the WPF framework-level controls that already enable scrollable regions) could compare available size to DesiredSize. 控制項接著可以將設定內部狀態的啟用中的捲軸UIUI該控制項。The control could then set an internal state that enabled scrollbars in the UIUI for that control. 或者,DesiredSize可能會造成在某些情況下也會忽略。Or, DesiredSize could potentially also be ignored in certain scenarios.

適用於

另請參閱