好的,这种行为似乎很奇怪。我有一个ListBox绑定到字符串的IBindingList。在它旁边的DataGridView中,我添加了一个绑定到同一List的组合框列,并在DataBindingComplete
事件中使用此代码。
gridPallets.Columns.Remove("Truck")
Dim Col = gridPallets.Columns.Add(New Windows.Forms.DataGridViewComboBoxColumn())
Dim CBC As Windows.Forms.DataGridViewComboBoxColumn = gridPallets.Columns(Col)
CBC.HeaderText = "Truck"
CBC.Name = "Truck"
CBC.DataPropertyName = "Truck"
CBC.DataSource = PackingList.Trucks
表单加载时,ListBox数据源设置为相同的列表对象。
lstTrucks.DataSource = PackingList.Trucks
奇怪的是,每当我在网格的任何一行中更改组合框选择时,列表框中的选定项目都会更改为匹配。不会发生相反的行为。我没有在代码中做任何事情来故意促进这一点。
是什么导致这个?这对我没有任何意义。
更新,来自lstTrucks.SelectedValueChanged事件的堆栈跟踪,
> CKProject.dll!CKProject.UI.cntPackingList.lstTrucks_SelectedValueChanged(Object sender = {SelectedItem = "Truck 2"}, System.EventArgs e = {System.EventArgs}) Line 221 Basic
System.Windows.Forms.dll!System.Windows.Forms.ListControl.OnSelectedValueChanged(System.EventArgs e) + 0x68 bytes
System.Windows.Forms.dll!System.Windows.Forms.ListBox.OnSelectedValueChanged(System.EventArgs e) + 0x11 bytes
System.Windows.Forms.dll!System.Windows.Forms.ListBox.OnSelectedIndexChanged(System.EventArgs e) + 0x19 bytes
System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int value) + 0x155 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x49 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x180 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.Position.set(int value) + 0x5a bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewComboBoxEditingControl.OnSelectedIndexChanged(System.EventArgs e) + 0x11 bytes
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.WndProc(ref System.Windows.Forms.Message m) + 0x3d7 bytes
这是IBindingList的代码:
<Serializable()>
Public Class GenericBindingList(Of T)
Inherits List(Of T)
Implements System.ComponentModel.IBindingList
Implements ITypedList
Implements ICloneable
Const _AllowNew As Boolean = True
Const _AllowEdit As Boolean = True
Const _AllowRemove As Boolean = True
Const _SupportsSorting As Boolean = False
Const _SupportsChangeNotification As Boolean = True
Const _SupportsSearching As Boolean = True
Shadows Sub Add(Item As T)
MyBase.Add(Item)
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemAdded, IndexOf(Item)))
End Sub
Shadows Sub Remove(item As T)
Dim DeleteIndex As Integer = IndexOf(item)
MyBase.Remove(item)
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemDeleted, DeleteIndex))
End Sub
<NonSerialized()> _
Private properties As PropertyDescriptorCollection
Private m_Indexes As New Hashtable
Public Sub AddIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex
If Not m_Indexes.ContainsKey([property]) Then
m_Indexes.Add([property], New Hashtable)
End If
End Sub
Public Sub RemoveIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex
m_Indexes.Remove([property])
End Sub
Public Overridable Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
Dim item As T = Activator.CreateInstance(Of T)()
Add(item)
Dim index As Integer = IndexOf(item)
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemAdded, index))
Return item
End Function
Public ReadOnly Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
Get
Return _AllowEdit
End Get
End Property
Public ReadOnly Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
Get
Return _AllowNew
End Get
End Property
Public ReadOnly Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
Get
Return _AllowRemove
End Get
End Property
Public Sub ApplySort(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
If SupportsSorting Then
Throw New NotImplementedException
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))
Else
Throw New NotSupportedException
End If
End Sub
Public Function IBindingListFind(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find
If m_Indexes.Contains([property]) Then
If CType(m_Indexes([property]), Hashtable).Contains(key) Then
Return CType(CType(m_Indexes([property]), Hashtable)(key), Integer)
End If
End If
Return -1
End Function
Public ReadOnly Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
Get
If SupportsSorting Then
Throw New NotImplementedException
Else
Return False
End If
End Get
End Property
Public Event ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged
Public Sub RaiseItemChanged(ByVal value As Object)
If value Is Nothing Then Return
Dim index As Integer = Me.IndexOf(value)
If index >= 0 Then
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemChanged, index))
End If
End Sub
Public Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
If SupportsSorting Then
Throw New NotImplementedException
RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))
Else
Throw New NotSupportedException
End If
End Sub
Public ReadOnly Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
Get
If SupportsSorting Then
Throw New NotImplementedException
Else
Return ListSortDirection.Ascending
End If
End Get
End Property
Public ReadOnly Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
Get
If SupportsSorting Then
Throw New NotImplementedException
Else
Return Nothing
End If
End Get
End Property
Public ReadOnly Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
Get
Return _SupportsChangeNotification
End Get
End Property
Public ReadOnly Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
Get
Return _SupportsSearching
End Get
End Property
Public ReadOnly Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
Get
Return _SupportsSorting
End Get
End Property
Public Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties
Dim pdc As PropertyDescriptorCollection = Nothing
If listAccessors Is Nothing OrElse listAccessors.Count = 0 Then
' Return properties in sort order
If properties Is Nothing Then
properties = TypeDescriptor.GetProperties(GetType(T), New Attribute() {New BrowsableAttribute(True)})
End If
pdc = properties
Else
' Return child list shape
pdc = Windows.Forms.ListBindingHelper.GetListItemProperties(listAccessors(0).PropertyType)
End If
Return pdc
End Function
Public Function GetListName(listAccessors() As System.ComponentModel.PropertyDescriptor) As String Implements System.ComponentModel.ITypedList.GetListName
Return GetType(T).Name
End Function
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim out As New GenericBindingList(Of T)
Me.ForEach(Sub(x) out.Add(DataInterface.GenericClone(x)))
Return out
End Function
End Class
PackingList.Trucks
是GenericBindingList(Of String)