Skip to content

Fix CarouselView PreviousPosition updating when inserting items #29617

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 6 commits into
base: main
Choose a base branch
from

Conversation

Shalini-Ashokan
Copy link
Contributor

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!

Issue Details

In the CarouselView control, when items are added at index 0, the PreviousPosition property in the PositionChangedEventArgs does not update correctly.

Root Cause

When items are inserted at index 0, explicitly setting the position to the current item position (currentItemPosition) prevents the actual position from changing. As a result, the PositionChanged event is not triggered, and PreviousPosition remains incorrect.

Description of Change

To correctly update the position and trigger the PositionChanged event, it update the PreviousPosition value correctly.

Validated the behavior in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed

Fixes #29524

Output ScreenShot

Before After
29524-BeforeFix.mov
29524-AfterFix.mov

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

Hey there @@Shalini-Ashokan! 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 22, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label May 22, 2025
// Updates position to match current item when inserting items, correctly tracking previous position values.
if (currentItemPosition != carouselPosition && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView)
{
if (InitialPositionSet && currentItemPosition > -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can nest this two conditions in a single one, but would be better to create a variable to increase the readability:

 var shouldUpdatePosition = 
     currentItemPosition != carouselPosition &&
     ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepItemsInView &&
     InitialPositionSet && 
     currentItemPosition > -1;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz, I have updated the code section based on your suggestions.

Margin = new Thickness(20),
};

insertButton.Clicked += (sender, e) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Could include another button to remove the current item and verify the PositionChanged event parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz, I have included the test case for the remove action.

@Shalini-Ashokan Shalini-Ashokan marked this pull request as ready for review May 27, 2025 12:29
@Shalini-Ashokan Shalini-Ashokan requested a review from a team as a code owner May 27, 2025 12:29
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[iOS] Previous Position in PositionChangedEventArgs Not Updating Correctly in CarouselView(CV2)
2 participants