Skip to content

Fixed [Windows] KeepLastItemInView Behavior Not Working as Expected in CarouselView #29550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

NanthiniMahalingam
Copy link
Contributor

@NanthiniMahalingam NanthiniMahalingam commented May 16, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root Cause

  • The CarouselView does not correctly identify the last item when KeepLastInView is enabled due to the presence of fake (looping) items.
  • In the OnItemsVectorChanged method, the item count returned includes these fake items, which prevents the actual last item from being recognized.
  • When dynamically resizing the window, the ItemsView position changes based on the CenterVisibleIndex value within the CarouselScrolled method. As a result, the current item scrolls away from the last item unexpectedly.

Description of Change

  • Updated the OnItemsVectorChanged method to use the CollectionViewSource.View of ItemCount to retrieve the actual items of the CarouselView, avoiding fake items.
  • This ensures that the correct last item is identified and displayed when KeepLastInView is set.
  • Restricted the ItemsView.ScrollTo call in UpdateCurrentItem method when ItemsView.Position is equal to the current item’s position due to items view position changed based on CenterVisibleIndex value inside the CarosuelScrolled method which caused that the current items has been scrolled from the last item while dynamically resizing the window.

Validated the behaviour in the following platforms

Issues Fixed

Fixes #29420

Output

Windows

Before After
Issue29420_Before.mp4
Issue29420_After.mp4

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label May 16, 2025
Copy link
Contributor

Hey there @@NanthiniMahalingam! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label May 16, 2025
@jsuarezruiz jsuarezruiz added platform/windows 🪟 area-controls-collectionview CollectionView, CarouselView, IndicatorView labels May 19, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@NanthiniMahalingam NanthiniMahalingam marked this pull request as ready for review May 19, 2025 15:24
@Copilot Copilot AI review requested due to automatic review settings May 19, 2025 15:24
@NanthiniMahalingam NanthiniMahalingam requested a review from a team as a code owner May 19, 2025 15:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the KeepLastItemInView behavior in CarouselView on Windows by adjusting how the last item is identified and preventing unwanted scroll operations when the carousel is resized.

  • Updated the item count calculation in OnItemsVectorChanged to avoid using fake looping items.
  • Restricted the ScrollTo call in UpdateCurrentItem when the current item is already in view.
  • Added test cases in both shared tests and the host app to validate the behavior on Windows (and other platforms).

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29420.cs Added test case for KeepLastItemInView on platforms failing the behavior
src/Controls/tests/TestCases.HostApp/Issues/Issue29420.cs Host test implementation verifying the fixed behavior
src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs Modified item count logic to differentiate between fake looping items and actual items
src/Controls/src/Core/Handlers/Items/CarouselViewHandler.Windows.cs Added a safeguard in UpdateCurrentItem to avoid unnecessary scroll operations

@@ -133,7 +133,8 @@ void OnItemsVectorChanged(global::Windows.Foundation.Collections.IObservableVect
if (sender is not ItemCollection items)
return;

var itemsCount = items.Count;
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding an inline comment clarifying why VirtualView is being checked against CollectionView and what types are expected; this will help future maintainers understand the intended behavior when selecting between items.Count and ItemCount.

Suggested change
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
// Use items.Count for CollectionView to account for this behavior; otherwise, use ItemCount for other types.

Copilot uses AI. Check for mistakes.

@@ -381,8 +381,10 @@ void UpdateCurrentItem()

var currentItemPosition = GetItemPositionInCarousel(ItemsView.CurrentItem);

Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It would be helpful to add a brief comment explaining the rationale for including the check against ItemsView.Position, so that the intention to prevent unnecessary scrolling when the current item is already visible is clearly documented.

Suggested change
// If the current item is already visible (ItemsView.Position == currentItemPosition),
// avoid unnecessary scrolling to improve performance and user experience.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows 🪟
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Windows] KeepLastItemInView Behavior Not Working as Expected in CarouselView
2 participants