好吧,当一个类实现 时INotifyPropertyChanged
,该类会在每个事件上使用一个属性等于的对象BindingList<T>
引发它的事件。ListChanged
PropertyChanged
ListChangedEventArgs
ListChangedType
ItemChanged
有了这些知识,我觉得你应该能够完成你想要的。(其中ListChangedType == ListChangedType.ItemChanged
,将 的索引NewIndex
处的对象传递BindingList<T>
给您的服务。)
更新:下面是我编写的示例应用程序,用于演示通过以下方式修改项目时引发ListChanged
事件:DataGridView
类:一个简单的CustomObject
实现INotifyPropertyChanged
Imports System.ComponentModel
Public Class CustomObject
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
OnPropertyChanged("Name")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
类:一个绑定到 a的DataBindingTestForm
基本Form
类DataGridView
BindingList(Of CustomObject)
Imports System.ComponentModel
Public Class DataBindingTestForm
Private WithEvents _customObjects As New BindingList(Of CustomObject)
Private Sub DataBindingTestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj As New CustomObject
obj.Name = "John"
_customObjects.Add(obj)
obj = New CustomObject
obj.Name = "Bill"
_customObjects.Add(obj)
MainDataGridView.DataSource = _customObjects
End Sub
Private Sub _customObjects_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _customObjects.ListChanged
If Not e.ListChangedType = ListChangedType.ItemChanged Then Return
Notify("ListChanged event fired. Index: {0}, Value: {1}", e.NewIndex, _customObjects(e.NewIndex).Name)
End Sub
Private Sub Notify(ByVal format As String, ByVal ParamArray args() As Object)
MsgBox(String.Format(format, args))
End Sub
End Class
在运行上述演示应用程序时,当我更改单元格中的值DataGridView
(例如,从“John”到“George”)时,会出现一个消息框,其中包含以下文本:
ListChanged event fired. Index: 0, Value: George
自己尝试一下,如果您遇到任何进一步的问题,请告诉我。