问题描述
我试图在我的 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!