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