FrameworkElement.ArrangeOverride(Size) 方法

定义

提供布局的“排列”传递的行为。 类可以重写此方法,以定义自己的“排列”传递行为。

protected:
 virtual Size ArrangeOverride(Size finalSize) = ArrangeOverride;
Size ArrangeOverride(Size const& finalSize);
protected virtual Size ArrangeOverride(Size finalSize);
function arrangeOverride(finalSize)
Protected Overridable Function ArrangeOverride (finalSize As Size) As Size

参数

finalSize
Size

父级中最后一个区域,此对象应使用该区域来排列自身及其子级。

返回

在布局中排列元素之后使用的实际大小。

示例

此示例实现 ArrangeOverride 以自定义自定义面板实现的“排列”传递逻辑。 特别要注意代码的以下方面:

  • 循环访问子级。
  • 对于每个子级,调用 Arrange,使用 Rect ,其中 HeightWidth 基于 DesiredSizeXY 基于特定于面板的逻辑。
  • 在本例中,返回其大小 (,此简单面板返回固定大小,而不是在) 累计排列的 Rect 值度量值时计算的大小。
// 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);
}
'Second arrange all children and return final size of panel 
Protected 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 
    '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

注解

此方法具有默认实现,该实现为大多数 FrameworkElement 派生类执行内置布局。 每当通过内部布局逻辑或你自己的应用代码调用 Arrange 时,ArrangeOverride 都提供 Arrange 的行为,包括你自己的其他类的任何 ArrangeOverride 方法。 如果要生成模板化控件,则 ArrangeOverride 逻辑定义控件的特定“排列”传递布局逻辑。

应用运行时元素如何通过布局过程的常规设计分为两个步骤:“度量”传递,然后是“排列”传递。 想要自定义布局处理的“排列”阶段的控制作者 (或面板作者) 应替代 ArrangeOverride。 实现模式应在每个可见子对象上调用 Arrange ,并将每个子对象的最终所需大小作为 finalRect 参数传递。 如果未调用 Arrange ,则不呈现子对象。

几个现有的非密封类提供此方法的替代实现。 突出的包括 StackPanelGrid。 通常,ArrangeOverride 的行为会生成 一个 finalSize ,它不违反放置在布局容器本身上的任何用户定义的值。 例如, finalSize 通常不大于容器的 HeightWidth,它考虑了影响内容区域的 MarginPadding 值。 专门具有超出容器大小的场景的控件可能会返回更大的值,但使用该控件的任何人都必须考虑到由此导致的剪辑和定位问题。 ArrangeOverride 实现传递给每个子对象的 Arrange 的值通常是上一个 Measure 调用在 DesiredSize 中设置的值。

适用于

另请参阅