[英]Binding the selected value from a combobox to a member of a class
我有一个绑定到一个类的实例的组合框。 我需要获取组合框的用户选择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"
尝试
Settings.selectedRobotID = ((robot)cmbDevices.SelectedItem).ID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.