共用方式為


UIElement.DesiredSize 屬性

定義

取得此 UIElement 在版面配置程式的量值階段期間計算的大小。

public:
 property Size DesiredSize { Size get(); };
Size DesiredSize();
public Size DesiredSize { get; }
var size = uIElement.desiredSize;
Public ReadOnly Property DesiredSize As Size

屬性值

UIElement 在版面配置程式的量值階段期間計算的大小。

範例

此範例會查詢 DesiredSize 做為 ArrangeOverride 實作子反復專案的一部分。

// 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

備註

當您實作 ArrangeOverrideMeasureOverride等配置行為覆寫時,DesiredSize 通常會檢查為其中一個度量因素。 視父容器的配置邏輯而定,DesiredSize 可能會受到完全遵守、可能套用 DesiredSize 的條件約束,而且這類條件約束也可能變更父元素或子項目的其他特性。 例如,支援可捲動區域的控制項 (,但選擇不要衍生自已啟用可捲動區域的控制項,) 可以比較可用的大小與 DesiredSize。 然後,控制項可以設定內部狀態,以在該控制項的 UI 中啟用捲軸。 或者,可以忽略 DesiredSize,而且元素一律會取得由其他考慮所調整大小的版面配置,例如檢查附加屬性值。

DesiredSize 不會包含有用的值,除非元素上至少執行一個版面配置「量值」階段。

DesiredSize 實際上只適用于當您定義自己的版面配置覆寫方法時使用。 如果您只想在執行時間對應用程式 UI 中的元素大小感興趣,您應該改用 ActualWidthActualHeight 屬性。 如果元素受到動態版面配置技術的影響,例如star網格線儲存格的大小調整,您可能會以這種方式檢查大小。 只有在請務必在配置執行之後才依賴 ActualWidthActualHeight 值:例如,在 Loaded 事件中,或由使用者動作觸發,只有在 UI 一開始轉譯之後才會觸發。

適用於

另請參閱