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,以自訂自訂面板實作的「排列」傳遞邏輯。 請特別注意下列程式碼層面:

  • 逐一查看子系。
  • 針對每個子系,使用Rect呼叫Arrange,其中HeightWidth是以DesiredSize為基礎,而XY是以面板特有的邏輯為基礎。
  • 在此案例中,會傳回其大小 (,這個簡單面板會傳回固定大小,而不是計算在累積排列 的 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 衍生類別執行內建配置。 ArrangeOverride 會提供 Arrange的行為,每當由內部配置邏輯或您自己的應用程式程式碼呼叫 Arrange 時,包括您自己針對其他類別使用的任何 ArrangeOverride 方法。 如果您要產生樣板化控制項,ArrangeOverride 邏輯會定義控制項的特定「排列」傳遞配置邏輯。

當您的應用程式執行時,元素如何經歷版面配置程式的一般設計分成兩個步驟:「量值」階段,然後是「排列」階段。 控制項作者 (或面板作者) 想要自訂版面配置處理的「排列」階段,應該覆寫 ArrangeOverride。 實作模式應該在每個可見的子物件上呼叫 Arrange ,並將每個子物件的最終所需大小傳遞為 finalRect 參數。 如果未呼叫 Arrange ,則不會轉譯子物件。

數個現有的非密封類別提供此方法的覆寫實作。 醒目顯示的專案包括 StackPanelGrid。 一般而言,ArrangeOverride 的行為會產生 finalSize ,不會違反任何放置在配置容器本身的使用者定義值。 例如, finalSize 通常不會大於容器 的 HeightWidth,會考慮影響內容區域的 MarginPadding 值。 特別具有超過容器大小的案例的控制項可能會傳回較大的值,但使用該控制項的任何人,都必須考慮其所產生的裁剪和定位問題。 ArrangeOverride 實作針對每個子物件傳遞至Arrange的值,通常是先前Measure呼叫在DesiredSize中設定的值。

適用於

另請參閱