0

I have an ObservableCollection which I need to replace via a reload button. During trying this out I found out that the CollectionChanged event fires even though the variable "myCollection" was nullified in "ReLoadData" (see code example below) and assigned a new ObservableCollection where I did not add any event handler to its CollectionChanged member:

    public partial class MainWindow : Window
    {
        private ObservableCollection<string> myCollection = 
           new ObservableCollection<string>();

        public MainWindow()
        {
           InitializeComponent();

           myCollection.CollectionChanged += new    
           System.Collections.Specialized.NotifyCollectionChangedEventHandler(
           myCollection_CollectionChanged);
        }

        //Invoked in button click handler:
        private void ReLoadData()
        {
           ObservableCollection<string> newCollection =
              new ObservableCollection<string>();

           //Filling newCollection with stuff...

           //Marks old collection for the garbage collector
           myCollection = null;
           myCollection = newCollection;          
        }

        void myCollection_CollectionChanged(
           object sender,   
           System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
           //Set breakpoint on statement or one of the braces.
        }

        private void AddItem(object sender, RoutedEventArgs args)
        {

           //Why does the following fire CollectionChanged
           //although myCollection was nullified in
           //ReLoadAuctionData() before?
           myCollection.Add("AddedItem");

        }

   }

I suspect this may have something to do with how the assignment operator is implemented in C#, but as far as I read it can not be overridden in C#, so I have no clue how to explain the above behaviour... Does anyone know this?

juniper
  • 311
  • 5
  • 13
  • It's pretty strange. Are you sure that you really call for ReLoadData() method on button click? I just copied this code and added this to the constructur: `myCollection.Add("lalala"); ReLoadData(); myCollection.Add("bobobo");`. And your handler doesn't invoke on second `Add` method. I think that there is some error in your button click handler.. – acrilige Mar 31 '13 at 17:08
  • I'm suspecting that whoever is 'filling' the collection - is still connected to the old 'collection' - i.e. regardless of the INotifyCollectionChanged, that's just for inside collection - you still need to 'notify' any 'subscribers' that the 'whole' collection has changed - and that using INotifyPropertyChanged - i.e. on that 'anulling' (which is unnecessary btw.) you need to call OnPropertyChanged("Collection") or whatever is named. – NSGaga-mostly-inactive Mar 31 '13 at 19:39

1 Answers1

0

(from the comment)

I'm suspecting that whoever is 'filling' the collection - is still connected to the old 'collection'

i.e. regardless of the INotifyCollectionChanged, that's just for inside collection - you still need to 'notify' any 'subscribers' that the 'whole' collection has changed - and that using INotifyPropertyChanged - i.e. on that 'anulling' (which is unnecessary btw.) you need to call OnPropertyChanged("Collection") or whatever is named.

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51