简体   繁体   English

将组合框中的选定值绑定到类的成员

[英]Binding the selected value from a combobox to a member of a class

I have a combobox that is bound to a an instance of a class. 我有一个绑定到一个类的实例的组合框。 I need to get the user's selection ID of the combobox and set a class property equal to it. 我需要获取组合框的用户选择ID并设置与之相等的类属性。

For example, here is the class: 例如,这是类:

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; }
    }
}

Now here is the code to bind the combobox to an instance of that class. 现在,这里是将组合框绑定到该类的实例的代码。 This display's the name of each robot in the array in the combobox. 此显示是组合框中阵列中每个机械手的名称。

    private void SetupDevicesComboBox()
    {
        robot[] robot = CommConfig.robot;
        cmbDevices.DataSource = robot;
        cmbDevices.DisplayMember = "name";
        cmbDevices.ValueMember = "ID";
    }

But now I can't seem to take what the user selects and use it. 但是现在我似乎无法接受用户选择和使用的内容。 How do I use the "ID" of what the user select's from the combobox? 如何使用用户从组合框中选择的内容的“ ID”?

Settings.selectedRobotID = cmbDevices.ValueMember;  //This just generates "ID" regardless of what is selected.

I also tried 我也试过

Settings.selectedRobotID = cmbDevices.SelectedItem.ToString();  //This just generates "CommConfig.robot"

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM