在组合框中显示空白而不是第一项

Display blank instead of first item in combobox

代码:

cmbItemType.DisplayMember = "Text";
cmbItemType.ValueMember = "Value";

var items = new[] {
        new { Text = "Text1", Value = "1"},  
        new { Text = "Text2", Value = "2"}
        };

cmbItemType.DataSource = items;

上面的代码显示了两个项目,但是如何在字段中显示一个空白项目。现在在加载时,它显示 Text1 而不选择。我想显示 空白项 而不是 Text1.

注意空白项不应添加到列表中,因此在选择combobox时,我不希望Text1显示文本上方看到空白项。谢谢。

将项目设置为数据源后:

cmbItemType.SelectedIndex = -1;

只需将 ComboBoxSelectedIndex 设置为 -1,这将向您显示 ComboBox,但不会从其项目列表中选择任何项目。

cmbItemType.DisplayMember = "Text";
cmbItemType.ValueMember = "Value";

var items = new[] {
    new { Text = "Text1", Value = "1"},  
    new { Text = "Text2", Value = "2"}
};
cmbItemType.DataSource = items;
cmbItemType.SelectedIndex = -1; 

注:

You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.

您可能有兴趣查找有关所选索引的更多信息 here