2

I have a XamDataGrid which is bound to an observable collection. As the XamDataGrid is editable, records can be added/edited/deleted. I have implemented CollectionChanged & PropertyChanged events.

The CollectionChanged event contains the following code:

        if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Add)
        {
            if (e.OldItems != null)
            {
                // Detach the event handler from current instance;
                foreach (BusinessTelephone oldItem in e.OldItems)
                {
                    if (oldItem is INotifyPropertyChanged)
                    {
                        (oldItem as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(PhoneDetails_PropertyChanged);
                    }
                }
            }

            if (e.NewItems != null)
            {
                // Attach the event handler to the new instance;
                foreach (BusinessTelephone newItem in e.NewItems)
                {
                    if (newItem is INotifyPropertyChanged)
                    {
                        (newItem as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(PhoneDetails_PropertyChanged);
                    }
                }
            }
        }

All of this is working fine.

I have a strange problem which is given below.

When I add a record in the grid and delete it from the grid, I am removing the item from the collection by looping through the collection

Ex: PhoneDetailsCollection.Remove(item);

Now, when I add another record, the CollectionChanged event is not getting fired.

Did I miss something here? Any help is highly appreciated...

CAMOBAP
  • 5,011
  • 8
  • 53
  • 84
Vikram
  • 39
  • 5

1 Answers1

0

e.Action could be something other than NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Add. It might be raising the event with "Reset", "Move", or "Replace". I'd double check that you are handling the event no matter which action is happening.

eoldre
  • 1,048
  • 16
  • 24