dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

Problem updating CollectionView ItemSource, when the modification is triggered by one of the items in the list #22122

Open nicop85 opened 7 months ago

nicop85 commented 7 months ago

Description

Think of a scenario where you have a list of persons and you want yo pin/unpin some of them as your favorites. You will have a CollectionView with it's ItemSource set to an ObservableCollection of persons. Each one of this persons will have a name and a boolean flag to know if they are favorite or not. Then, in the ItemTemplate, each row will display the name of the person and a button to pin/unpin them as favorite.

The idea is that when the user click the button to pin/unpin a person, it will change the status of the isFavorite flag and it should update the list to show all the favorite persons first and then the rest of them. A very common use case scenario of a list with some kind of preference in the items.

The logic behind the scenes, is that when a user clicks the button to pin/unpin a person, it will first change the IsFavorite flag of that person and then execute a method that performs an ObservableCollection.Clear() and then re fill it again with the updated persons.

The problem I've found is that the update in the item flag is performed well, but the Iist itself doesn't get reorder the right way showing the favorite ones first. This seems to only occur when the update was triggered by an item of the list, because if I put a button to refresh the list and execute the same code to re fill the ObservableCollection, everything works as expected.

Am I missing something or did I make any mistake in the code? Or is it a bug in the CollectionView?

I have tested this in all the 8.0.x versions of MAUI and even in some of 7.0.x, so I guess is something that was there for quite a while or perhaps I'm doing something wrong.

Any advice, please let me know

Steps to Reproduce

  1. Clone the repository I'm linking below
  2. Execute the code in desktop or mobile
  3. Try to pin a person different than the first listed one
  4. It should be shown at the top of the list but it will remain in its position
  5. Make the gesture to refresh the list if you are in mobile, or click the refresh button if you are in desktop
  6. The list will then be shown as expected

Link to public reproduction project repository

https://github.com/nicop85/TestCollectionViewRefresh.git

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

Android 11 and up, Windows 10.0.17763.0 and up

Did you find any workaround?

Not yet

Relevant log output

No response

kevinxufei commented 7 months ago

Can repro this issue at android/Windows platforms on the latest 17.10 Preview 5 (8.0.21/8.0.7).

PureWeen commented 7 months ago

/similarissues

github-actions[bot] commented 7 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

nicop85 commented 7 months ago

None of those similar issues seems to be related to this one