gpt4 book ai didi

vb.net - 通知对另一个类或对象的更改

转载 作者:行者123 更新时间:2023-12-04 18:22:04 26 4
gpt4 key购买 nike

我在一个小修复中,我有一个如下的类(class)。

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 中的某些 x 值当 ItemDetails 中的任何一个发生变化时 items 中的对象.

你能帮我解决一下吗?

最佳答案

尝试使用 BindingList(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

关于vb.net - 通知对另一个类或对象的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10455290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com