Pasting your code and mapping the event like follows works for me static void Main(string args) { EntityCollection col = new EntityCollection(); EntityCollectionObserver colObserver = new EntityCollectionObserver(col); colObserver. CollectionChanged += colObserver_CollectionChanged; col. Add("foo"); } static void colObserver_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Console.
WriteLine("Entity Collection Changed"); }.
Pasting your code and mapping the event like follows works for me. Static void Main(string args) { EntityCollection col = new EntityCollection(); EntityCollectionObserver colObserver = new EntityCollectionObserver(col); colObserver. CollectionChanged += colObserver_CollectionChanged; col.
Add("foo"); } static void colObserver_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Console. WriteLine("Entity Collection Changed"); }.
Works! Not sure what I was doing wrong previously. – Daniel Skinner Apr 6 at 8:45 As it turns out, it doesn't work in all cases.
See my answer below for a solution that worked for me. – Daniel Skinner Apr 13 at 13:37.
Whilst it worked in the simple use case noted by @Aron, I couldn't get it to work properly in my actual application. As it turns out, and for reasons I'm not sure - the inner IBindingList of an EntityCollection somehow, somewhere, can get changed. The reason my observers weren't being called is because they were looking for changes on an old IBindingList that wasn't even being used by the EntityCollection any more.
Here is the hack that got it working for me: public class EntityCollectionObserver : ObservableCollection where T : class { private static List> InnerLists = new List>(); public EntityCollectionObserver(EntityCollection entityCollection) : base(entityCollection) { IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList()); l. ListChanged += new ListChangedEventHandler(OnInnerListChanged); foreach (var x in InnerLists. Where(x => x.
Item2 == entityCollection && x. Item1! = l)) { x.
Item3. ObserveThisListAswell(x. Item1); } InnerLists.
Add(new Tuple(l, entityCollection, this)); } private void ObserveThisListAswell(IBindingList l) { l. ListChanged += new ListChangedEventHandler(OnInnerListChanged); } private void OnInnerListChanged(object sender, ListChangedEventArgs e) { base. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.
Reset)); } }.
Man, that seems like a lot of work... I wonder how that internal list is getting changed. You were not changing it externally somewhere? So weird.
– Aron Apr 22 at 20:20 I never worked out when or why it was being changed. Since there is no was to monitor when the inner list is changed even the above solution is unreliable. I've migrated to STE's for now and am happily making use of TrackableCollection.
– Daniel Skinner Apr 26 at 11:55.
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.
Does anyone have any ideas how I might observe changes to an EntityCollection?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.