Skip to main content
 

Gets the size that this element computed during the measure pass of the layout process.

Namespace:   System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)

public Size DesiredSize { get; }
public:
property Size DesiredSize {
	Size get();
}
member DesiredSize : Size with get
Public ReadOnly Property DesiredSize As Size

Property Value

Type:

The computed size, which becomes the desired size for the arrange pass.

The value returned by this property will only be a valid measurement if the value of the property is true.

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 instead, but this depends on your implementation). Depending on the scenario, might be fully respected by your implementation logic, constraints on might be applied, and such constraints might also change other characteristics of either the parent element or child element. For 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 . The control could then set an internal state that enabled scrollbars in the UI for that control. Or, could potentially also be ignored in certain scenarios.

The following example shows as part of a MeasureOverride implementation. Notice how Measure is called immediately prior to obtaining . This assures that holds a legitimate value.

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
    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 ;
    }
.NET Framework
Available since 3.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top