Tomasz
Tomasz

Reputation: 2061

StackPanel height exceed parent Grid height

I have situation like this:

<UserControl>
    <Grid x:Name="fullGrid" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="innerGrid" VerticalAlignment="Stretch">
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <StackPanel x:Name="leftSide" VerticalAlignment="Stretch">
                <ScrollViewer VerticalAlignment="Stretch">
                    <ItemsControl/ VerticalAlignment="Stretch">
                </ScrollViewer>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>

Problem is that leftSide stackPanel height is higher than it's parent: innerGrid height.

I was debugging it in Snoop and it seems that StackPanel just ignore it's VerticalAlignment property.

I would like to avoid setting Heigh={Binding ElementName=xxx, Path=ActualHeight} because I have some additional Margins inside, and it break the view.

How can I handle that?

Upvotes: 2

Views: 1855

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61339

ScrollViewer fills its parent. StackPanel wants to size to its children, and so tells them they have as much space as they want (and then "shrinks to fit").

So, StackPanel tells ScrollViewer it can have all the space in the world, which it happily takes. There is no way to stop this besides doing a binding as you describe or setting an absolute height.

So the simple solution is: remove the StackPanel. Then the ScrollViewer will take up the space the Grid assigns it.

Upvotes: 3

Related Questions