[英]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.