Export (0) Print
Expand All

UIElement.DesiredSize Property

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

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

public Size DesiredSize { get; }

Property Value

Type: System.Windows.Size
The size that this UIElement computed during the measure pass of the layout process.

DesiredSize values are set by the layout system as part of calls to Measure.

DesiredSize values are useful if you are implementing layout override behavior, in particular during the arrange pass. Depending on the scenario, DesiredSize might be fully respected by your layout logic, constraints on DesiredSize might be applied, and such constraints might also change other characteristics of either the parent or child in layout. For example, a control that supports scrollable regions (but chooses not to derive from 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 layout scenarios.

You would not typically get the value of DesiredSize outside of a context where your code has already called Measure on that same element as part of the measure pass of layout, or called something that initiates a full layout pass such as UpdateLayout. Otherwise, there is no guarantee that the value held by DesiredSize is relevant.

The following example queries DesiredSize as part of the child iteration for an ArrangeOverride implementation.


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

// Calculate point origin of the Block you are in
protected Point GetOrigin(int blockNum, int blocksPerRow, Size itemSize)
{
    // Get row number (zero-based)
    int row = (int)Math.Floor(blockNum / blocksPerRow);

    // Get column number (zero-based)
    int column = blockNum - blocksPerRow * row;

    // Calculate origin
    Point origin = new Point(itemSize.Width * column, itemSize.Height * row);
    return origin;

}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft