【问题标题】:ComboBox with Binding on SeclectedItem unable to change selection在 SeclectedItem 上具有绑定的 ComboBox 无法更改选择
【发布时间】:2017-07-24 10:14:17
【问题描述】:

我有一个简单的表单,它使用 DataBinding 绑定到对象集合。最初我不希望 ComboBox 显示选择,因此我将其选定索引设置为 -1。但是,当 ComboBox 被选中时,我无法在不选择值的情况下取消选择它。

如何在不选择值的情况下取消选择 ComboBox(选择其他控件)?

要重新创建,创建一个新的 winform,添加一个 ComboBox 和一个 TextBox,然后使用以下代码:

Imports System.Collections.Generic

Public Class Form1

    Public Property f As Person

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim db As New People
        ' ComboBox1.CausesValidation = False
        ComboBox1.DataSource = db
        ComboBox1.DisplayMember = "Name"
        ComboBox1.DataBindings.Add("SelectedItem", Me, "f", False, DataSourceUpdateMode.OnPropertyChanged)
        ComboBox1.SelectedIndex = -1
    End Sub

End Class

Public Class Person
    Public Property Name As String
End Class

Public Class People
    Inherits List(Of Person)

    Public Sub New()
        Me.Add(New Person With {.Name = "Dave"})
        Me.Add(New Person With {.Name = "Bob"})
        Me.Add(New Person With {.Name = "Steve"})
    End Sub
End Class

当窗体启动时,应该选择 ComboBox,而不能选择 TextBox。

我发现在 ComboBox 上将 CausesValidation 切换为 False 可以解决问题,但会破坏 DataBinding。

【问题讨论】:

  • 我认为列表绑定ComboBox(更一般地ListControl)不能取消选择,因为CurrencyManager不允许将Position设置为-1,而底层列表Count是> 0.
  • 解决方法添加一个具有无效值的 NONE 项。
  • 允许用户选择无效值并不是一个好的解决方案

标签: .net vb.net winforms


【解决方案1】:

找到了解决办法! (谢谢伊万!)

如果 ComboBox 在 Validating 时的 SelectedIndex 为 -1,则暂时禁用绑定。

        AddHandler ComboBox1.Validating, AddressOf DisableBindingWhenNothingSelected
        AddHandler ComboBox1.Validated, AddressOf DisableBindingWhenNothingSelected

    ''' <summary>
    ''' Temporarily disables binding when nothing is selected.  
    ''' </summary>
    ''' <param name="sender">Sender of the event.</param>
    ''' <param name="e">Event arguments.</param>
    ''' <remarks>A list bound ComboBox (and more generically ListControl) cannot be deselected, 
    ''' because CurrencyManager does not allow setting the Position to -1 when the underlying list Count is > 0. 
    ''' This should be bound to the Validating and Validated events of all ComboBoxes that have an initial SelectedIndex of -1.</remarks>
    Protected Sub DisableBindingWhenNothingSelected(sender As Object, e As EventArgs)
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        If cb.SelectedIndex = -1 Then
            If (cb.DataBindings.Count = 0) Then Exit Sub
            If cb.DataBindings(0).DataSourceUpdateMode = DataSourceUpdateMode.Never Then
                cb.DataBindings(0).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
            Else
                cb.DataBindings(0).DataSourceUpdateMode = DataSourceUpdateMode.Never
            End If
        End If
    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2012-09-25
    • 2013-06-09
    • 2021-12-18
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多