2

我在一个小修复中,我有一个如下的课程。

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

prime当. _ ItemDetails_items

你能帮我解决一下吗?

4

2 回答 2

3

尝试使用 aBindingList(of T)代替,然后您可以侦听更改事件:

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

这将需要您的类来实现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
于 2012-05-04T20:39:59.627 回答
1

每当其任何属性发生更改时,ItemDetails 类都需要引发事件。我建议在 ItemDetails 类上实现 INotifyPropertyChanged 接口,但您可以实现自己的事件。然后,您需要将事件处理程序添加到每个 ItemDetails.PropertyChanged 事件,因为它已添加到列表中,并从每个项目中删除处理程序,因为它已从列表中删除。例如:

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
于 2012-05-04T20:36:36.060 回答