Question

In a Windows 8.1 application (.NET 4.5) I have (across two templates, because of Binding) a Grid which contains a StackPanel which contains a ListView. The thing is, the ListView doens't scale with available space, instead shows a scroll bar.

<Grid DataContext="{Binding}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/> <!-- IF I set MinHeight here, it grows -->
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0"/>
    <ListView
            Grid.Row="1"
            Margin="10"
            ItemsSource="{Binding ementas}"
            ItemTemplate="{StaticResource temp1}"
            SelectionMode="None"
            IsItemClickEnabled="False"
            IsSwipeEnabled="False">
    </ListView>
</Grid>

Template for the ListView

<StackPanel Width="310" Margin="5,10,5,10" DataContext="{Binding}">
    <TextBlock Text="{Binding data}" Style="{StaticResource TitleTextBlockStyle}"/>
    <ListView SelectionMode="None"
            IsItemClickEnabled="False"
            ItemsSource="{Binding pratos}"
            ItemTemplate="{StaticResource temp2}"
            IsSwipeEnabled="False">
    </ListView>
</StackPanel>

And the template for the second ListView

<TextBlock Text="{Binding descricao}"
    Margin="5"
    Style="{StaticResource BodyTextBlockStyle}"
    TextWrapping="WrapWholeWords"/>

Screenshot: Screenshot

I'd like for the ListView with the scrollbar to have it's size scale to fit content, and only scroll if it runs out of screen space.

Was it helpful?

Solution

This is an extremely common problem... the StackPanel does not resize its contents to fit the parent control like a Grid does. The solution is simple... don't use a StackPanel except for the simplest of lining up a few controls duties. Instead of this, use a Grid which will automatically fit all space provided by parent controls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top