信息
您所需的主题如下所示。但此主题未包含在此库中。

UIElement.DesiredSize 属性

2013/12/13

获取此 UIElement 在布局过程的测量处理过程中计算的大小。

Namespace:  System.Windows
程序集:  System.Windows(位于 System.Windows.dll 中)

声明
Public ReadOnly Property DesiredSize As Size

属性值

类型: System.Windows.Size
UIElement 在布局过程的测量处理过程中计算的大小。

DesiredSize 值由布局系统作为对 Measure 的调用的一部分设置。

如果正在实现布局重写行为(尤其是在排列处理过程期间),则 DesiredSize 值很有用。根据具体情况,您的布局逻辑可能会充分考虑 DesiredSize,可能应用 DesiredSize 上的约束,并且此类约束可能也会更改布局中父对象或子对象的其他特征。例如,支持可滚动区域(但选择不从已启用可滚动区域的控件派生)的控件可以将可用大小与 DesiredSize 进行比较。该控件于是可以在其 UI 中设置启用滚动条的内部状态。或者,在某些布局情况下也可能忽略 DesiredSize

您通常不能在以下这样的上下文之外获取 DesiredSize 的值:其中您的代码已对该同一元素调用 Measure 作为布局度量处理过程的一部分,或已调用启动完整布局处理过程的某些内容,如 UpdateLayout。否则,无法保证 DesiredSize 包含的值是相关的。

下面的示例查询 DesiredSize 以作为 ArrangeOverride 实现的子迭代的一部分。


'Second arrange all children and return final size of panel 
Protected Overloads 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 
    'We're 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


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: