jeff porter
jeff porter

Reputation: 6620

ListView : how to always scroll to the bottom

I have a list view that I want to scroll to the bottom as I add items into the "Items" list.

As I add items they appear in the ListView, but when I reach the limit of the screen, the list remains showing the top section and new items are added to the bottom. If I scroll down I can see the new items. I'd like it to auto scroll to the bottom so that I can always see the latest items in the list.

<ListView 
         x:Name="lvBasketContent" 
         Grid.Row="1" 
         ItemsSource="{Binding Items}"
         ItemContainerStyle="{StaticResource ListViewItemStyle1}" 
         HorizontalContentAlignment="Stretch"
         VerticalContentAlignment="Bottom" 
         SelectionMode="None" 
         IsSwipeEnabled="False" 
         VerticalAlignment="Top"
         >

Can anyone help me out please?

Upvotes: 0

Views: 3152

Answers (2)

Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

Do you can try put this in your code ? Each time you add an item to your listbox , try call it

//Add an item in the listbox
lvBasketContent.Items.Add(...);

//...

//Scroll to bottom
lvBasketContent.SelectedIndex = lvBasketContent.Items.Count -1
lvBasketContent.ScrollIntoView(lvBasketContent.SelectedItem)

Upvotes: 0

Gusdor
Gusdor

Reputation: 14334

You need to create a custom behavior or derived implementation of ListView.

This class should monitor the ItemsSource collection for changes and call ListViewBase.ScrollIntoView(Object), passing in the item that you want to show. In your case, this may be the last one added.

I recommended the behavior as it keeps your code modular as you can use it on any listview in your solution by changing the xaml only.

I'm not going to write the code for you as behaviors are a very useful technique to learn first hand. The first link should give you all you need to get cracking.

Upvotes: 1

Related Questions