我有一个绑定到类实例的组合框。我需要获取组合框的用户选择ID,并设置一个等于它的类属性。
例如,这是类:
public class robot
{
private string _ID;
private string _name;
private string _configFile;
[XmlElement("hardware")]
public hardware[] hardware;
public string ID
{
get { return _ID; }
set { _ID = value; }
}
public string name
{
get { return _name; }
set { _name = value; }
}
public string configFile
{
get { return _configFile; }
set { _configFile = value; }
}
}
现在这里是将组合框绑定到该类实例的代码。此显示是组合框中阵列中每个机器人的名称。
private void SetupDevicesComboBox()
{
robot[] robot = CommConfig.robot;
cmbDevices.DataSource = robot;
cmbDevices.DisplayMember = "name";
cmbDevices.ValueMember = "ID";
}
但现在我似乎无法接受用户选择并使用它的内容。如何使用用户从组合框中选择的“ID”?
Settings.selectedRobotID = cmbDevices.ValueMember; //This just generates "ID" regardless of what is selected.
我也试过
Settings.selectedRobotID = cmbDevices.SelectedItem.ToString(); //This just generates "CommConfig.robot"
答案 0 :(得分:0)
尝试
Settings.selectedRobotID = ((robot)cmbDevices.SelectedItem).ID;