0

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;
        }
4lex
  • 49
  • 10
  • 1
    You've subscribed to notifications for the wrong instance of `TrulyObservableCollection`. You've failed to provide a good [mcve], one which would include the `HelperClass.LoadItems()` method, but presumably that method returns an entirely new instance, rather than using the current value of `LoadingDataCollection`, so you need to subscribe to the event on _that_ new instance, or don't create a new instance, but instead load the items directly into the instance you've already created. See marked duplicate for additional details. – Peter Duniho Sep 09 '19 at 16:30
  • HelperClass.LoadItems() returns a new TrulyObservableCollection object with all the objects filled, maybe that could be the problem. Thanks Peter. – 4lex Sep 09 '19 at 16:40

1 Answers1

0

This code right here:

LoadingDataCollection = HelperClass.LoadItems();

Is assigning a new collection to the "LoadingDataCollection" variable and wiping out the fact that you assigned an event handler to the collection that previous occupied that variable.

If you change that code to:

LoadingDataCollection.AddRange(HelperClass.LoadItems()); 

It should work.

Gary Stewart
  • 164
  • 5
  • It says TrulyObservableCollection does not contain definition for AddRange should this be implemented in TrulyObservableCollection ? – 4lex Sep 09 '19 at 16:29
  • You can add your own AddRange function or check the link that Peter identified as being a duplicate of your question. – Gary Stewart Sep 09 '19 at 16:32