UIElement.DesiredSize Propiedad

Definición

Obtiene el tamaño que este UIElement calcula durante el paso de medida del proceso de diseño.

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

Valor de propiedad

Tamaño que este UIElement calculó durante el paso de medida del proceso de diseño.

Ejemplos

En este ejemplo se consulta DesiredSize como parte de la iteración secundaria para una implementación 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

Comentarios

DesiredSize normalmente se comprueba como uno de los factores de medida al implementar invalidaciones de comportamiento de diseño, como ArrangeOverride o MeasureOverride. En función de la lógica de diseño del contenedor primario, DesiredSize podría ser totalmente respetado, se podrían aplicar restricciones en DesiredSize y estas restricciones también podrían cambiar otras características del elemento primario o el elemento secundario. Por ejemplo, un control que admite regiones desplazables (pero elige no derivar de los controles que ya habilitan regiones desplazables) podría comparar el tamaño disponible con DesiredSize. Después, el control podría establecer un estado interno que habilitó barras de desplazamiento en la interfaz de usuario para ese control. O bien, DesiredSize se puede omitir y el elemento siempre obtiene un diseño que tiene el tamaño de otras consideraciones, como comprobar los valores de propiedad adjunta.

DesiredSize no contendrá un valor útil a menos que se haya ejecutado al menos un paso de "Medida" del diseño en el elemento .

DesiredSize solo está pensado para su uso cuando se definen sus propios métodos de invalidación de diseño. Si solo estás interesado en el tamaño de un elemento de la interfaz de usuario de la aplicación en tiempo de ejecución, debes usar las propiedades ActualWidth y ActualHeight en su lugar. Es posible que esté comprobando el tamaño de esta manera si un elemento está influenciado por técnicas de diseño dinámico, como star ajuste de tamaño de las celdas grid. Confíe en los valores ActualWidth y ActualHeight solo en situaciones que estén seguros de estar después de que se haya ejecutado el diseño: por ejemplo, en Eventos cargados o desencadenados por acciones de usuario que solo son posibles después de que la interfaz de usuario se haya representado inicialmente.

Se aplica a

Consulte también