I’m currently working on an app that needs to show the pictures and videos held on the user’s device for them to select from. I came across this great post by @XamBoy that I have used to get the media and save it in an ObservableRangeCollection.
NB an ObservableRangeCollection is an object that inherits from ObservableCollection, created by James Montemagno and available through his MvvmHelpers NuGet. Everything described below can be changed to extend just the ObservableCollection instead.
Now, what I need to do is allow a user to select one or many of the available media and show a neat little tick icon accordingly.
Except ObservableCollection (and therefore ObservableRangeCollection) does not recognise when an item has been changed. Per the Microsoft docs:-
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
Fortunately, I’ve come across this before in another project. First, I created a custom object which extends ObservableRangeCollection.
Then I amend my MediaAsset model to implement INotifyPropertyChanged and ensure the IsSelected property fires the OnPropertyChangedEvent.
Finally, I declare my collection of MediaAssets using my new object.
public ItemsChangeObservableRangeCollection MediaAssets
{
get => _mediaAssets;
set
{
_mediaAssets = value;
RaisePropertyChanged(() => MediaAssets);
}
}
Now, when I click on an image and update the IsSelected property for that asset I get my expected functionality:-
