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 가 계산한 크기입니다.

예제

다음은 ArrangeOverride 구현에 대한 자식 반복의 일부로 DesiredSize를 쿼리하는 예제입니다.

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

설명

DesiredSize는 일반적으로 ArrangeOverride 또는 MeasureOverride 와 같은 레이아웃 동작 재정의를 구현할 때 측정 요소 중 하나로 확인 됩니다. 부모 컨테이너의 레이아웃 논리에 따라 DesiredSize는 완전히 존중되고 DesiredSize에 대한 제약 조건이 적용될 수 있으며 이러한 제약 조건은 부모 요소 또는 자식 요소의 다른 특성을 변경할 수도 있습니다. 예를 들어 스크롤 가능한 영역을 지원하지만 이미 스크롤 가능한 영역을 사용하도록 설정한 컨트롤에서 파생되지 않도록 선택하는 컨트롤은 사용 가능한 크기를 DesiredSize와 비교할 수 있습니다. 그런 다음 컨트롤은 해당 컨트롤에 대해 UI에서 스크롤 막대를 사용하도록 설정하는 내부 상태를 설정할 수 있습니다. 또는 DesiredSize를 무시할 수 있으며 요소는 항상 연결된 속성 값 확인과 같은 다른 고려 사항에 따라 크기가 조정되는 레이아웃을 가져옵니다.

요소에서 레이아웃의 "측정값" 패스가 하나 이상 실행되지 않는 한 DesiredSize에는 유용한 값이 포함되지 않습니다.

DesiredSize는 고유한 레이아웃 재정의 메서드를 정의할 때만 사용하기 위한 것입니다. 런타임에 앱의 UI에 있는 요소의 크기에만 관심이 있는 경우 ActualWidthActualHeight 속성을 대신 사용해야 합니다. 요소가 그리드 셀의 star 크기 조정과 같은 동적 레이아웃 기술의 영향을 받는 경우 이러한 방식으로 크기를 확인할 수 있습니다. 레이아웃이 실행된 후에야 하는 상황에서만 ActualWidthActualHeight 값을 사용합니다( 예: 로드된 이벤트 또는 UI가 처음 렌더링된 후에만 가능한 사용자 작업에 의해 트리거됨).

적용 대상

추가 정보