I need to react to the content of an ObservableCollection's property changing. I define the property like this:
public static readonly BindableProperty MyCollectionProperty = BindableProperty.Create(
nameof(MyContentView.MyCollection),
typeof(ObservableCollection<object>),
typeof(MyContentView),
propertyChanged: OnMyCollectionPropertyChanged);
public ObservableCollection<object> MyCollection
{
get => (ObservableCollection<object>)GetValue(MyContentView.MyCollectionProperty);
set => SetValue(MyContentView.MyCollectionProperty, value);
}
private static void OnMyCollectionPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
{
// Do Something
}
The method OnMyCollectionPropertyChanged is called when the whole bound collection object changes (i.e. a completely new collection) but of course not when the collection's content changes.
What is the correct way to react to the events of the INotifyCollectionChanged interface implemented by ObservableCollection? Do I need to manually sign up for them when the collection object is assigned or is there a syntax of BindableProperty.Create() which allows to do so directly, something similar to the propertyChanged method?
I make a code sample of Custom
BindableProperty
for TypeObservableCollection
for your reference.ContentView:
Useage:
Xaml:
Code behind: