Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
 ArrangeOverride Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
FrameworkElement..::.ArrangeOverride Method

Provides the behavior for the Arrange pass of Silverlight layout. Classes can override this method to define their own Arrange pass behavior.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Protected Overridable Function ArrangeOverride ( _
    finalSize As Size _
) As Size
Visual Basic (Usage)
Dim finalSize As Size
Dim returnValue As Size

returnValue = Me.ArrangeOverride(finalSize)
C#
protected virtual Size ArrangeOverride(
    Size finalSize
)

Parameters

finalSize
Type: System.Windows..::.Size
The final area within the parent that this object should use to arrange itself and its children.

Return Value

Type: System.Windows..::.Size
The actual size used once the element is arranged.

This method has a default implementation that performs built-in layout for most Silverlight FrameworkElement classes.

Notes to Inheritors:

For Silverlight (and also for WPF), the technique by which elements are sized and positioned in a layout is divided into two steps: a Measure pass, then an Arrange pass. This technique is explained in more detail in the topic Silverlight Layout System.

Control authors (or panel authors) who want to customize the Arrange pass of layout processing should override ArrangeOverride. The implementation pattern should call Arrange on each visible child object, and pass the final desired size for each child object as the finalRect parameter. Containers that contain visible child objects should call Arrange on each child, otherwise the child object will not be rendered.

Several existing Silverlight classes provide override implementations of this method. Prominent ones include: StackPanel..::.ArrangeOverride and Grid..::.ArrangeOverride.

Typically, the behavior of ArrangeOverride produces a finalSize that does not violate any user-defined values placed on the layout container itself, such as Height and Width, accounting for Margin or Padding values that affect the content area, and so on.

The value that a ArrangeOverride implementation passes to Arrange for each child object is generally the value that is set in DesiredSize by the previous Measure pass on that object. However, this might vary depending on the specific layout functionality of the parent.

The following example implements ArrangeOverride to customize the Arrange pass logic for a custom panel implementation. Note in particular these aspects of the code:

  • Iterates over children.

  • For each child, calls Arrange, using a Rect where Height and Width are based on DesiredSize, and X and Y are based on logic that is specific to the panel.

  • Returns its size (in this case, this simple panel returns a fixed size rather than a size calculated on accumulating the arranged Rect value measurements).

Visual Basic
'Second arrange all children and return final size of panel 
Protected Overloads Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size
    'Get the collection of children 
    Dim mychildren As UIElementCollection = Children

    'Get total number of children 
    Dim count As Integer = mychildren.Count

    'Arrange children 
    'We're only allowing 9 children in this panel. More children will get a 0x0 layout slot. 
    Dim i As Integer
    For i = 0 To 8

        'Get (left, top) origin point for the element in the 3x3 block 
        Dim cellOrigin As Point = 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. 
        Dim dw As Double = mychildren(i).DesiredSize.Width
        Dim dh As Double = mychildren(i).DesiredSize.Height


        mychildren(i).Arrange(New Rect(cellOrigin.X, cellOrigin.Y, dw, dh))
    Next
    For i = 9 To count - 1

        'Give the remaining children a 0x0 layout slot 
        mychildren(i).Arrange(New Rect(0, 0, 0, 0))
    Next


    'Return final size of the panel 
    Return New Size(300, 300)
End Function

'Calculate point origin of the Block you are in 
Protected Function GetOrigin(ByVal blockNum As Integer, ByVal blocksPerRow As Integer, ByVal itemSize As Size) As Point
    'Get row number (zero-based) 
    Dim row As Integer = CInt(Math.Floor(blockNum / blocksPerRow))

    'Get column number (zero-based) 
    Dim column As Integer = blockNum - blocksPerRow * row

    'Calculate origin 
    Dim origin As New Point(itemSize.Width * column, itemSize.Height * row)
    Return origin

End Function
C#
//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;

}

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker