question

AnasGuibene avatar image
0 Votes"
AnasGuibene asked AnasGuibene edited

How to make my listview scroll to the last item

Hello,

I have a listview that contains messages, I want to scroll down the list to the last message when I open the page.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

I want to scroll down the list to the last message when I open the page.

ListView provides the ScrollTo method will could help you scroll the ListView to the item.

Check the code:

<ListView x:Name="listView" ItemsSource="{Binding DataCollection}">
    ...
</ListView>

public partial class TestPage : ContentPage
{
    TestPageViewModel viewModel;
    public TestPage()
    {
        InitializeComponent();

        viewModel = new TestPageViewModel();
        BindingContext = viewModel;
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        listView.ScrollTo(viewModel.DataCollection[viewModel.DataCollection.Count-1], ScrollToPosition.End, true);
    }
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnasGuibene avatar image
0 Votes"
AnasGuibene answered AnasGuibene edited

It Works like this too

 if (MessagesListView.ItemsSource != null)
             {
                 var lastItem = MessagesListView.ItemsSource.Cast<object>().LastOrDefault();
                 MessagesListView.ScrollTo(lastItem, ScrollToPosition.End, true);
             }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.