2010-01-18 86 views
0

我有一个绑定到类的实例的组合框。我需要获取用户的组合框的选择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

尝试

Settings.selectedRobotID = ((robot)cmbDevices.SelectedItem).ID; 
+0

酷奏效。我发现这也起作用。 Settings.selectedRobotID = cmbDevices.SelectedValue.ToString(); – 2010-01-18 21:59:01