Problem with ScrollTo

Lloyd Sheen 1,491 Reputation points
2023-12-14T20:06:14.25+00:00

I have a collectionview with many items. I have implemented a search which will find an item by a person's name and then scroll to that person.

Now it gets weird. Certain seachs cause the scrollto to work and the item is scrolled to the top. But other values find the item and use same code to scrollto and the collectionview goes into a up and down motion (does not scroll a full item) and the only thing the UI will handle is a close on the Title bar.

The code that is being used to scroll is the following :

public void SearchForPlayer(string value)
{
    bool found = false;
    int idx = 0;
    foreach (PlayerYearStats s in displayScorers)
    {
        if (s.fullName.ToLower().Contains(value.ToLower()))
        {
            theCollView.ScrollTo(idx, position: ScrollToPosition.Start);
            found = true;
            break;
        }
        idx++;
    }
    if (found)
        HideKeyBoard();
}


Shown above is using the index of the item to scroll but using s to scrollto the item has the same problem. Commenting out the HideKeyBoard also does not change the execution. I can see no difference (other than the data which is not used except to search for the item) between items that work and ones that don't.

I also have a Scrolled event defined for the CollectionView and after the code above this event keeps on getting called. On a breakpoint there is no call stack so I have no idea what is causing the scroll to keep on going. The definition of the CollectionView is :

        <CollectionView
            Grid.Row="2"
            Grid.Column="1"
            x:Name="scorersCV"
            ItemsSource="{Binding displayScorers}"
            ItemTemplate="{StaticResource scorersTemplate}"
            HorizontalScrollBarVisibility="Always"
            Scrolled="collScroll">


Also tried with the HorizontalScrollBarVisibility deleted and no change. Using 17.7.7 as 17.8.3 does not create Release builds so I had to go back to previous version.

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Lloyd Sheen 1,491 Reputation points
    2023-12-15T22:31:16.2533333+00:00

    Ok so I found the problem. If you do not set the animate option on the ScrollTo to false then the scrolling gets into a loop with more than 150 items. Change it to false and the item pops up and that is it.


  2. Richard Dufour 10 Reputation points
    2024-05-29T18:37:24.79+00:00

    I was never able to get ScrollTo on Windows to work.

    Works on iOS, Windows, forget it, tried just about everything.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.