OnPropertyChanged on an ObservableCollection doesn’t refresh a CollectionView content

The issue

I have in my ViewModel an ItemsChangedObservableRangeCollection (which is basically an ObservableRangeCollection from the MVVMHelpers package, with some extra methods to add an event when an item from the collection is changed), and in my Xaml page a CollectionView bound to this collection. To display my data, I use a few converters, which works as expected when I open the page for the first time.

My issue happens when I come back to this page later on, and I want to refresh my data. Despite the data being cleared and refreshed, and despite the OnPropertyChanged event being called either on the Collection itself or on the items contained (I even tried calling it manually from a temporary button added for debugging), the CollectionView isn’t updated, and the Converters aren’t even called.

Just for a bit of extra context, the data in my ObservableRangeCollection aren’t actually changing : the Collection contains User data, and it’s the role of the Converters to use the user ids and fetch some Scores which are subject to change ; that’s why I’m trying to call OnPropertyChanged manually to force the Converters to refresh.

My code :

An extract of the ContentPage containing the CollectionView with the different Converters in use :

<ContentPage.Behaviors>
    <toolkit:EventToCommandBehavior Command="{Binding RefreshVMCommand}" EventName="Appearing"/>
</ContentPage.Behaviors>

<!-- [...] -->

<CollectionView x:Name="participantsListView" 
ItemsSource="{Binding CompetitorsBySelectedGroup }" 
SelectionMode="None" >
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid x:DataType="model:Competitor" HeightRequest="76" >
                
                <!--Grid Row and Column Definitions-->

                <Label Grid.Column="0" Text="{Binding Number }"/>
                <Label Grid.Column="1" Text="{Binding FullName }"/>

                <Image Grid.Column="2" Source="task_list_check.svg" IsVisible="{Binding ., Converter={StaticResource IsResultScoreNotNullConverter}}"/>

                <Border Grid.Column="3" IsVisible="{Binding ., Converter={StaticResource IsResultScoreNotNullConverter}}">
                    <Label Text="{Binding ., Converter={StaticResource GetResultScore}}"/>
                </Border>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

The code used to refresh the data, called from the ViewModel on the page’s OnAppearing :

[RelayCommand]
public void RefreshVM()
{
    CompetitorsBySelectedGroup?.Clear();
    var competitorList = DataManager.Instance.AppData.GetCompetitorsByCurrentGroup();
    CompetitorsBySelectedGroup.AddRange(competitorList ?? []);

    // Tentative to notify collection changed
    OnPropertyChanged(nameof(CompetitorsBySelectedGroup));

    // Tentative to notify directly items changed
    foreach (INotifyPropertyChanged item in CompetitorsBySelectedGroup)
    {
        OnPropertyChanged(nameof(item));
    }
}

I noticed something interesting, the CollectionView is actually refreshed when instead of clearing my ObservableCollection (CompetitorsBySelectedGroup?.Clear()), I just redefine it (CompetitorsBySelectedGroup = []), but for some adressing reasons I’d prefer to avoid doing it.

The code for my ItemsChangedObservableRangeCollection :

namespace MauiUtils
{
    /// <summary>
    ///     This class adds the ability to refresh the list when any property of
    ///     the objects changes in the list which implements the INotifyPropertyChanged. 
    /// </summary>
    /// <typeparam name="T"/>
    public class ItemsChangedObservableRangeCollection<T> : ObservableRangeCollection<T> where T : INotifyPropertyChanged
    {
        public delegate void ItemChangedEventHandler(object source, EventArgs args);

        /// <summary>
        /// Event fired when an item of the collection is updated
        /// </summary>
        public event ItemChangedEventHandler ItemChanged;

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                RegisterPropertyChanged(e.NewItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                UnRegisterPropertyChanged(e.OldItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                UnRegisterPropertyChanged(e.OldItems);
                RegisterPropertyChanged(e.NewItems);
            }
            base.OnCollectionChanged(e);
        }

        protected override void ClearItems()
        {
            UnRegisterPropertyChanged(this);
            base.ClearItems();
        }

        private void RegisterPropertyChanged(IList items)
        {
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void UnRegisterPropertyChanged(IList items)
        {
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnItemChange();
        }

        protected virtual void OnItemChange()
        {
            ItemChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật