xamarin.formsmvvmmauimaui-community-toolkit

Difficulty removing and updating grouped items in ObservableCollection in .NET MAUI


Title: Difficulty removing and updating items in ObservableCollection with CollectionView in .NET MAUI

I'm encountering an issue with removing and updating items in an ObservableCollection used with a CollectionView in my .NET MAUI application. Specifically, I have a CollectionView bound to an ObservableCollection, where CourseGroup represents groups of Courses grouped by Type (docs).

I'm unable to remove items properly from this CollectionView. Normally, removing items from an ObservableCollection triggers UI updates, but in this case, directly removing a Course within a CourseGroup does not trigger the ObservableCollection to update. I've attempted to refresh the start page automatically, but this introduces a brief loading delay that I'd prefer to avoid for a seamless user experience.

Has anyone encountered a similar issue or found a workaround for ensuring proper updates to the CollectionView when removing or updating items within the underlying CourseGroup objects? Any insights or suggestions would be greatly appreciated!

public ObservableCollection<CourseGroup> CourseGroups { get; } = new ObservableCollection<CourseGroup>();

Solution

  • I have just found a solution for this issue. Just make sure the class with the group implements ObservableCollection.

    Before:

    public class CourseGroup: List<Course>
    

    After:

    public class CourseGroup: ObservableCollection<Course>