jay_t55
jay_t55

Reputation: 11652

Scroll Vertically inside ListView when ListView is VerticalAlignment="Stretch"

How do we get the ListView to behave properly? I am trying to scroll vertically inside my ListView, and I was eventually able to get something to scroll vertically, but the ListView keeps expanding in height! and setting MaxHeight doesn't do anything.

    <StackPanel x:Name="ActivityLog" Margin="0, 201, 0, 0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListView x:Name="ActivityList" ItemsSource="{Binding Activities}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" HorizontalContentAlignment="Center" FontSize="14">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Message}" Background="{x:Null}" HorizontalAlignment="Stretch" Padding="5" TextAlignment="Center" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>
    </StackPanel>

I am at a loss. Please help. How do I get the listview to just scroll vertically only, and never change in height, and never display the horizontal scrollbars?

Upvotes: 1

Views: 508

Answers (1)

Fede
Fede

Reputation: 44038

Remove the StackPanel. That prevents the ListView (or any other children) to have a delimited size. That's why you're not getting any scrollbars, because the ListView has an infinite height, as layout-ed by the StackPanel.

Besides, having a StackPanel with only 1 child makes no sense. It's supposed to "stack" its children.

Edit: Also remove the ScrollViewer, as the ListView already has a ScrollViewer inside of it as per the default Template. Unless of course you had changed the Default ControlTemplate for that.

Upvotes: 3

Related Questions