Export (0) Print
Expand All

UIElement.DesiredSize Property

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; }

Property Value

Type: System.Windows.Size
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 IsMeasureValid property is true.

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). 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. 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 DesiredSize. The control could then set an internal state that enabled scrollbars in the UI for that control. Or, DesiredSize could potentially also be ignored in certain scenarios.

The following example shows DesiredSize as part of a MeasureOverride implementation. Notice how Measure is called immediately prior to obtaining DesiredSize. This assures that DesiredSize 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 ;
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft