locked
Add object to ObservableCollection doesn't trigger a PropertyChanged event? RRS feed

  • Question

  • In my ViewModel I have the following:

    Public Class UIMerchandise
    Implements INotifyPropertyChanged, IDataErrorInfo
    
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    
        If PropertyChangedEvent IsNot Nothing Then
    
            RaiseEvent PropertyChanged(Me, e)
    
            Select Case e.PropertyName
    
                Case Else
    
            End Select
    
        End If
    
    End Sub
    
    Private _Merchandises As Collections.ObjectModel.ObservableCollection(Of DC.SL.Services.WebServiceMerchandise.Merchandise)
    Public Property Merchandises() As Collections.ObjectModel.ObservableCollection(Of DC.SL.Services.WebServiceMerchandise.Merchandise)
        Get
            Return _Merchandises
        End Get
        Set(ByVal value As Collections.ObjectModel.ObservableCollection(Of DC.SL.Services.WebServiceMerchandise.Merchandise))
            _Merchandises = value
            OnPropertyChanged(New PropertyChangedEventArgs("Merchandises"))
        End Set
    End Property
    
    Private Sub TestMethod()
    
        Dim anItem As New DC.SL.Services.WebServiceMerchandise.Merchandise
        With anItem
            .ClassificationID = 1
            .ClassificationSubID = 2
            .Discount = 10
        End With
    
        Merchandises.Insert(0, anItem)
    
    End Sub
    
    Public Sub New()
    
        Me.Merchandises = New Collections.ObjectModel.ObservableCollection(Of DC.SL.Services.WebServiceMerchandise.Merchandise)
    
    TestMethod()
    
    End Sub
    
    End Class
    

    The OnPropertyChanged for "Merchandises" (yes I know there is no such word, for sample purpose) only fires on the New instance, it does NOT fire on the Merchandises.Insert (see in TestMethod()). As I understand it, ObservableCollection has INotifyCollectionChanged as part of it's class and the OnPropertyChanged should respond to any Add,Insert,Delete in ObservableCollection ... but it doesn't??

    What am I missing?

    Friday, November 9, 2012 4:35 PM

Answers

  • Hi Robin,

    Don't be too worried, you're not the first person to be confused by this.

    What needs to be understood is the difference between changing the items in the ObservableCollection, and changing the ObservableCollection itself.

    In your viewmodel, you are responding to a change of the collection itself. This will only fire if you assign something to the "Merchandises" property. So if you create a new ObservableCollection and assign it to Merchandises, you'll get the property changed event.

    The ObservableCollection itself has its own event called CollectionChanged. If you subscribe to that, you'll be able to catch insert and delete events on the collection.

    var myColl = new ObservableCollection();
    
    myColl.CollectionChanged += (s, e) =>
       {
          // Do stuff
       }

    The "e" argument of the event is a NotifyCollectionChangedEventArgs argument that can tell you what items changed.

    One other thing to remember is that people also get confused by changing a property of an item in the collection and expecting the collection to respond to that. Change a property of an item in the collection does not affect the collection itself.

    Hope that helps!

    Jason

    Saturday, November 10, 2012 11:14 PM