如何取消选择/空白数据绑定的组合框? SelectedIndex = -1不起作用 [英] How to deleselect / blank a databound ComboBox? SelectedIndex = -1 does not work
问题描述
我试图在Windows窗体应用程序中取消选择(空白显示)多个组合框。在我的应用程序中,我有一个Reset方法,该方法将每个组合的SelectedIndex设置为-1。我所有的组合框都是数据绑定的,即每个组合框都是使用数据源填充的。
I am trying to deselect (blank out) a number of combo-boxes in my windows forms application. In my application I have a Reset method that sets the SelectedIndex for each combo to -1. All of my combo-boxes are databound, i.e. each combo-box is populated using a datasource.
我注意到有时我的Reset方法有效,即取消选择当前选中的项目,然后使组合变为空白。但是,有时我会在尝试将其设置为-1之后立即选择第一个项目(SelectedIndex = 0)。从用户的角度来看,这似乎是一个错误,因为它并不总是清除表单。
I have noticed that sometimes my Reset method works, i.e. it deselects the currently selected item and blanks the combo. However, other times it chooses the first item (SelectedIndex = 0) straight after I attempt to set it to -1. From a users point of view this looks like a bug as it doesn't always "clear" the form.
根据MSDN:
要取消选择当前选定的项目,请将SelectedIndex设置为-1。如果该项是数据绑定项,则不能将ComboBox项的SelectedIndex设置为-1。
"To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item."
有人知道解决方法吗?
非常感谢
推荐答案
使用void和property的组合
Use combination of the void and property
comboBox.ResetText();
//to reset selected value
comboBox.SelectedIndex = -1;
这篇关于如何取消选择/空白数据绑定的组合框? SelectedIndex = -1不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!