surpavan
surpavan

Reputation: 1422

Inotify change to another class or object

I am in a small fix, I have a class as below.

Public Class Bill
    Public prime As BillPrime
    Public items As System.Collections.ObjectModel.ObservableCollection(Of ItemDetails)
    Public status As New BillStatus
    Public Sub New()
        prime = New BillPrime
        items = New System.Collections.ObjectModel.ObservableCollection(Of ItemDetails)
        status = New BillStatus
    End Sub
End Class

How can I update some x value in prime when there is a change in any of the ItemDetails object in items.

Could you please help on how can I come to a solution?

Upvotes: 2

Views: 1705

Answers (2)

LarsTech
LarsTech

Reputation: 81675

Try using a BindingList(of T) instead, then you can listen for a change event:

Imports System.ComponentModel

Public Class Bill
  Public prime As BillPrime
  Public WithEvents items As BindingList(Of ItemDetails)
  Public status As New BillStatus
  Public Sub New()
    prime = New BillPrime
    items = New BindingList(Of ItemDetails)
    status = New BillStatus
  End Sub

  Public Sub items_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)  Handles items.ListChanged
    prime.X = "something"
  End Sub
End Class

This would require your classes to implement INotifyPropertyChanged:

Public Class ItemDetails
  Implements INotifyPropertyChanged

  Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

  Private _DetailOne As String

  Property DetailOne() As String
    Get
      Return _DetailOne
    End Get
    Set(ByVal value As String)
      _DetailOne = value
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("DetailOne"))
    End Set
  End Property

End Class

Upvotes: 3

Steven Doggart
Steven Doggart

Reputation: 43743

The ItemDetails class will need to raise an event whenever any of its properties change. I would suggest implementing the INotifyPropertyChanged interface on the ItemDetails class, but you could implement your own event, instead. You will then need to add an event handler to each ItemDetails.PropertyChanged event as it is added to the list and remove the handler from each item as it is removed from the list. For instance:

Public Class Bill
    Public prime As BillPrime
    Public items As System.Collections.ObjectModel.ObservableCollection(Of ItemDetails)
    Public status As New BillStatus

    Public Sub New()
        prime = New BillPrime
        items = New System.Collections.ObjectModel.ObservableCollection(Of ItemDetails)
        AddHandler items.CollectionChanged, AddressOf items_CollectionChanged
        status = New BillStatus
    End Sub

    Private Sub items_CollectionChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
        For Each i as ItemDetails in e.NewItems
            AddHandler i.PropertyChanged, AddressOf item_PropertyChanged
        Next
        For Each i as ItemDetails in e.OldItems
            RemoveHandler i.PropertyChanged, AddressOf item_PropertyChanged
        Next
    End Sub

    Private Sub item_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
        'Do work
    End Sub
End Class

Upvotes: 1

Related Questions