Skip to content

Potential off-by-one error when using ScrollTo in CollectionView with a header. #18389

@bellascalzi1

Description

@bellascalzi1

Description

Programatically scrolling to the Count - 1 item index (ie the last item in the CollectionView) in a CollectionView using the ScrollTo method does not work when a header is present and instead scrolls to the second-to-last item. I would expect it to scroll to the last item in the CollectionView instead since the documentation states that items in a CollectionView are 0-indexed here is the link where the documentation says this

As a workaround, scrolling to index Count does scroll to the last item in the CollectionView.

Here's a recording demonstrating the behaviour and I also have a public repo with my reproduction code:

collectionview-scrollto-bug.webm

(Note I don't know the .NET version this is happening on, sorry! my dotnet workload list output is below and says I have .NET 8 installed but the TargetFramework in my .csproj file is net7.0)

Installed Workload Id      Manifest Version                     Installation Source
---------------------------------------------------------------------------------------------------
android                    34.0.0-rc.1.432/8.0.100-rc.1         VS 17.7.34031.279, VS 17.8.34112.27
maui-windows               8.0.0-rc.1.9171/8.0.100-rc.1         VS 17.7.34031.279, VS 17.8.34112.27
maui-maccatalyst           8.0.0-rc.1.9171/8.0.100-rc.1         VS 17.7.34031.279
maccatalyst                16.4.8825-net8-rc1/8.0.100-rc.1      VS 17.7.34031.279, VS 17.8.34112.27
maui-android               8.0.0-rc.1.9171/8.0.100-rc.1         VS 17.7.34031.279
maui-ios                   8.0.0-rc.1.9171/8.0.100-rc.1         VS 17.7.34031.279
ios                        16.4.8825-net8-rc1/8.0.100-rc.1      VS 17.7.34031.279, VS 17.8.34112.27

Steps to Reproduce

  1. Create a new .NET MAUI project
  2. Add a CollectionView to a page with some items and add a header as well, making sure to add a x:Name attribute to the CollectionView so it can be referenced in the code-behind
  3. Add a button somewhere (can be in the header) and a clicked event handler that executes <CollectionView Name>.ScrollTo(<CollectionView items name>.Count - 1);

The CollectionView will now scroll to the second-to-last item rather than the last item.

Link to public reproduction project repository

https://github.com/bellascalzi1/MauiCollectionViewScrollTo

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

I can see this issue in API 33

Did you find any workaround?

If you want to scroll to the last item use ScrollTo(Items.Count) instead for now.

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions