I am using this https://stackoverflow.com/a/5256827 drop-in class that sub-classes ObservableCollection that notifies when a item in the collection changes. It works raising the event.
public sealed class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += FullObservableCollectionCollectionChanged;
}
public TrulyObservableCollection(IEnumerable<T> pItems) : this()
{
foreach (var item in pItems)
{
this.Add(item);
}
}
private void FullObservableCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged;
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged;
}
}
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Here when an item changes it works.
System.Windows.MessageBox.Show("ItemPropertyChanged fired!");
NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender, IndexOf((T)sender));
OnCollectionChanged(args);
}
}
When an item changes ItemPropertyChanged is fired, but when I use the collection and subscribe a function to OnCollectionChanged the subscribed event is not being fired.
public TrulyObservableCollection<ModelObj> LoadingDataCollection;
public Loading()
{
InitializeComponent();
LoadingDataCollection = new TrulyObservableCollection<ModelObj>();
LoadingDataCollection.CollectionChanged += ContentCollectionChanged;
// Fill the collection
LoadingDataCollection = HelperClass.LoadItems();
}
public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Never reaches here.
System.Windows.MessageBox.Show("ContentCollectionChanged fired!");
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (ModelObj item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EntityViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (ModelObj item in e.NewItems)
{
//Added items
item.PropertyChanged += EntityViewModelPropertyChanged;
}
}
}
public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Also Never reaches here. Here I want to get the changed property and update it in the database MySQL
System.Windows.MessageBox.Show("EntityViewModelPropertyChanged");
}
Any idea why this could be happening?
EDIT: SOLVED
As Peter mentioned the problem was that I was subscribing to the wrong instance of TrulyObservableCollection. Instead of creating a new TrulyObservableCollection I just assigned the returned by the HelperClass.
public Loading()
{
InitializeComponent();
// This was the wrong piece
//LoadingDataCollection = new TrulyObservableCollection<ModelObj>();
// Asign the collection returned from HelperClass
LoadingDataCollection = HelperClass.LoadItems();
// Subscribe
LoadingDataCollection.CollectionChanged += ContentCollectionChanged;
}