UIElement.Arrange(Rect) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
public:
virtual void Arrange(Rect finalRect) = Arrange;
void Arrange(Rect const& finalRect);
public void Arrange(Rect finalRect);
function arrange(finalRect)
Public Sub Arrange (finalRect As Rect)
Parameters
- finalRect
- Rect
The final size that the parent computes for the child in layout, provided as a Rect value.
Examples
This example shows how you would use Arrange
within an ArrangeOverride implementation. The basic idea is that you should query DesiredSize on anything you attempt to call Arrange
on so that you have a value for finalRect
, unless your layout implementation has some specific design that alters or ignores the desired size before passing it as finalRect
.
// 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);
}
Remarks
The Arrange
call potentially reaches an ArrangeOverride implementation of that specific class. Otherwise, most FrameworkElement classes have an implicit default layout behavior for Arrange
.
Computation of initial layout positioning in a XAML UI consists of a Measure call and an Arrange
call, in that order. During the Measure
call, the layout system determines an element's size requirements using the availableSize
measurement. During the Arrange call, the layout system finalizes the size and position of an element's bounding box.
When a layout is first produced, it always has a Measure call that happens before Arrange
. However, after the first layout pass, an Arrange
call can happen without a Measure
preceding it. This can happen when a property that affects only Arrange
is changed (such as alignment), or when the parent receives an Arrange
without a Measure
.
A Measure call will automatically invalidate any Arrange
information. Layout updates generally occur asynchronously (at a time determined by the layout system). An element might not immediately reflect changes to properties that affect element sizing (such as Width).
Layout updates can be forced by app code rather than relying on the built-in layout system behavior by using the UpdateLayout method. However, that is not recommended. It is usually unnecessary and can cause poor performance if overused. In many situations where calling UpdateLayout
from app code might be appropriate because of changes to properties, the layout system will probably already be processing updates. The layout system also has optimizations for dealing with cascades of layout changes through parent-child relationships, and calling UpdateLayout
can work against such optimizations. Nevertheless, it's possible that layout situations exist in more complicated scenarios where calling UpdateLayout
is the best option for resolving a timing issue or other issue with layout. Just use it deliberately and sparingly.