0
我有一个简单的表单,它使用DataBinding绑定到一组对象。最初我不希望ComboBox显示选择,所以我将其选定索引设置为-1。但是,当组合框变为选中状态时,我无法取消选择它,而无需选取值。在SeclectedItem上绑定组合框无法更改选择
如何在不选取值的情况下取消选择ComboBox(选择其他控件)?
要重新创建,创建一个新的winform,添加一个组合框和文本框,然后使用此代码:当形式开始了ComboBox应选择
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上切换CausesValidation为False可以修复问题,但会中断DataBinding。
我认为结合'ComboBox'(和更一般'ListControl')不能被取消选择列表,因为'CurrencyManager'不允许设置的'Position'为-1时基础列表'Count'是> 0 。 –
解决方法添加无效值的NONE项目。 – Ramankingdom
允许用户选择无效值不是一个好的解决方案 –