我意识到程序启动时我的组合框总是空的。我必须单击旁边的箭头才能选择一个值。我们如何做到这一点,以便在程序启动时组合框将显示一个值?
问问题
22574 次
5 回答
7
您可以设置以下 4 个属性:
// Gets or sets the index specifying the currently selected item.
comboBox1.SelectedIndex = someIndex; //int
// Gets or sets currently selected item in the ComboBox.
comboBox1.SelectedItem = someItem; // object
// Gets or sets the text that is selected in the editable portion of a ComboBox.
comboBox1.SelectedText = someItemText; // string
// Gets or sets the value of the member property specified by the ValueMember property.
comboBox1.SelectedValue = someValue; // object
直接来自 MSDN 的评论行:http: //msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
于 2012-07-24T10:46:16.853 回答
4
如果您有一个组合框并想设置它的数据源,您可以这样做:
string[] items = new string[]{"Ram","Shyam"};
comboBox1.DataSource = items;
comboBox1.SelectedIndex = 0;
所以尝试将SelectedIndex
第一个索引设置为。
于 2012-07-24T10:49:36.760 回答
0
试试这个代码:
comboBox1.Items.Add("Test");
于 2012-07-24T10:43:55.593 回答
0
如果您将 WPF 与 MVVM 和INotifyPropertiesChanged
接口一起使用,则可以在绑定属性中设置后备值。
SelectedIndex="{Binding SelectedCountry.MultipleClassCountry, FallbackValue= 0}"
设置SelectedIndex = 0
应该选择您在组合框中的第一个项目。
于 2019-04-23T07:19:57.600 回答
0
Davenewza 的回答非常适合程序化方法(我强烈推荐)。另一种不太优雅但使用属性工具箱的方法是执行以下操作:
- 在设计视图中,单击有问题的组合框。
- 导航到外观-> 文本并输入您想要的任何字符串。
为了安全起见,我将输入一个与将在框中选择的内容相对应的值,以防止不需要的字符串传播到其他函数/变量。如果不小心处理,这个原因可能会引起很多麻烦,这就是为什么首选程序化方法的原因。
于 2017-06-13T18:35:38.510 回答