Элемент управления ListBox изменяет выбор при изменении выбора поля со списком DataGridView - PullRequest
0 голосов
/ 06 октября 2011

Хорошо, это поведение кажется очень странным. У меня есть ListBox, который связан с IBindingList строк. В DataGridView рядом с ним я добавил столбец со списком, связанный с тем же списком, с этим кодом в событии 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)

...