我有一个ComboBox
它有一个DataSource
这是一个List<CustomClass>
。现在在这个自定义类中,有一个属性被用作DisplayMember
和ValueMember
。价值成员和DropDownStyle.DropDown
现在,这一切工作得很好,当我使用DropDownStyle.DropDownList
。但如果将类型更改为DropDownStyle.DropDown
并在ComboBox
中输入自定义文本,DisplayMember
和ValueMember
会发生什么变化?那么DisplayMember
和ValueMember
会是什么呢?
简单的例子
// Create empty list of type CustomClass
List<CustomClass> comboBoxDataSource = new List<CustomClass>();
// Create a property for the selected item
CustomClass CurrentSelectedItem { get; set; }
// Use external function to fill the list with some data
LoadDataAndInsertIntoList(comboBoxDataSource);
// Set datasource, display- and value-member
cboComboBox.DataSource = comboBoxDataSource;
cboComboBox.DisplayMember = "DisplayName";
cboComboBox.ValueMember = "Value";
private void cboComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
// What happens here, if using DropDownStyle.DropDown and
// a custom text is entered, instead of the predefined options?
CurrentSelectedItem = (CustomClass)cboComboBox.SelectedItem;
}
也许你需要检查下拉或下拉列表,然后只使用不同的转换,如果是下拉也许((CustomClass)cboComboBox.SelectedItem).displaymember? –