mvvmmauihideobservablecollection

Is possibile to hide elementis from ObservableCollection? Net MAUI


I'm using MVVM and Ms Toolkit.

I'm showing a collection within a CollectionView, but I would like to hide deleted items (set by a flag on model).

This why I'm using a global "save" procedure in a tabs page but it would be nice to hide what I've deleted.

I've tried with Where(p => p.deleted == false) but:

  1. won't work on ObservableColletion
  2. return a IEnumerable

Searched Google & Docs already.


Solution

  • I've tried with Where(p => p.deleted == false) but:

    1.won't work on ObservableColletion

    2.return a IEnumerable

    Returning IEnumerable is not a problem, because it can be converted to list:

    https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist?view=net-8.0

    It will work on ObservableCollection, because you have constructor that takes list as parameter.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.observablecollection-1.-ctor?view=net-8.0#system-collections-objectmodel-observablecollection-1-ctor(system-collections-generic-list((-0)))

    Also, even if anything of the above was not true, you can always:

    1. Suppress notifications
    2. Clear collection and add items.
    3. Turn notifications on.

    I do it all the time.