Peter
Peter

Reputation: 489

When I extend a panel and make a simple ArrangeOverride the content starts in the middle instead of upper left corner

When I extend a panel and make as simple ArrangeOverride the content starts in the middle instead of upper left corner. New point 0,0 should make the content start at the upper left as far as I can see. Who can explain this behaviour?

When I scale the mainwindow the upperleft corner of the content (the text) keeps in the middle of the MainWindow

enter image description here

Public Class AvoidInfiniteSizePanel
    Inherits Panel

    Protected Overrides Function MeasureOverride(availableSize As Size) As Size
        If Me.Children.Count = 1 Then
            Dim Content = Me.Children(0)
            Content.Measure(New Size(Double.MaxValue, Double.MaxValue))

            Dim MyDesiredSize As Windows.Size = New Size(Math.Max(Content.DesiredSize.Width, MinimalWidth), Math.Max(Content.DesiredSize.Height, MinimalHeight))
            Return MyDesiredSize
        Else
            Return MyBase.MeasureOverride(availableSize) 'Default gedrag
        End If
    End Function

    Protected Overrides Function ArrangeOverride(finalSize As Size) As Size
        If Me.Children.Count = 1 Then
            Me.Children(0).Arrange(New Rect(New Point(0, 0), finalSize))
        Else
            Return MyBase.ArrangeOverride(finalSize) 'Default gedrag
        End If    

    End Function
End Class

and the XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:infiniteSizeTester"
                Title="MainWindow" Height="125" Width="230">
    <Grid>
        <local:AvoidInfiniteSizePanel>
            <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" >Why is this in the center instead of at position 0,0</TextBlock >
        </local:AvoidInfiniteSizePanel>
    </Grid>
</Window>

Upvotes: 2

Views: 688

Answers (1)

Clemens
Clemens

Reputation: 128013

You have missed to return the finalSize value from your ArrangeOverride. Hence the Panel reports its size as (0, 0). As it is centered in its parent Grid, the TextBlock appears at the center position.

Protected Overrides Function ArrangeOverride(finalSize As Size) As Size
    If Me.Children.Count = 1 Then
        Me.Children(0).Arrange(New Rect(New Point(0, 0), finalSize))
        Return finalSize 'here
    Else
        Return MyBase.ArrangeOverride(finalSize)
    End If    
End Function

Anyway, I would suggest to simplify your code and write the Panel like this:

Public Class CustomPanel
    Inherits Panel

    Protected Overrides Function MeasureOverride(availableSize As Size) As Size
        Dim desiredSize As New Size(MinimalWidth, MinimalHeight)
        For Each child As UIElement In InternalChildren
            child.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
            desiredSize.Width = Math.Max(desiredSize.Width, child.DesiredSize.Width)
            desiredSize.Height = Math.Max(desiredSize.Height, child.DesiredSize.Height)
        Next child
        Return desiredSize
    End Function

    Protected Overrides Function ArrangeOverride(finalSize As Size) As Size
        For Each child As UIElement In InternalChildren
            child.Arrange(New Rect(finalSize))
        Next child
        Return finalSize
    End Function
End Class

Upvotes: 1

Related Questions