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

注解

实现 Layout 行为替代(如 ArrangeOverride 或 MeasureOverride )时,通常将 DesiredSize 检查为度量因素之 。 根据父容器的布局逻辑,可以完全遵循 DesiredSize,可能应用对 DesiredSize 的约束,并且此类约束也可能更改父元素或子元素的其他特征。 例如,支持可滚动区域的控件 (但选择不从已启用可滚动区域的控件派生,) 可以将可用大小与 DesiredSize 进行比较。 然后,控件可以设置内部状态,以便在 UI 中为该控件启用滚动条。 或者,可以忽略 DesiredSize,并且元素始终获取一个布局,该布局的大小由其他注意事项(例如检查附加的属性值)调整。

DesiredSize 将不包含有用的值,除非至少有一个“度量”的布局传递已在 元素上运行。

DesiredSize 实际上仅用于定义自己的布局替代方法时使用。 如果只是对运行时应用 UI 中元素的大小感兴趣,则应改用 ActualWidthActualHeight 属性。 如果某个元素受到动态布局技术(如网格单元格大小star调整)的影响,则可以通过这种方式检查大小。 仅当在布局运行后肯定为时,才依赖于 ActualWidthActualHeight 值:例如,在 加载 事件中,或由用户操作触发,而用户操作仅在 UI 最初呈现后才能执行。

适用于

另请参阅