I'm using EntityFramework database first in an application. I would like somehow to be notified of changes to an EntityCollection
in my ViewModel. It doesn't directly support INotifyCollectionChanged
(why?) and I haven't been successful in finding another solution.
Here's my latest attempt, which doesn't work because the ListChanged
event doesn't appear to get raised:
public class EntityCollectionObserver<T> : ObservableCollection<T>, INotifyCollectionChanged where T : class
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public EntityCollectionObserver(EntityCollection<T> entityCollection)
: base(entityCollection)
{
IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList());
l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);
}
private void OnInnerListChanged(object sender, ListChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
Does anyone have any ideas how I might observe changes to an EntityCollection
?
Dan
have you tried handling AssociationChanged Occurs when a change is made to a related end. (Inherited from RelatedEnd.)
It gives arguments showing whether an element was added or deleted and also exposes the element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With