使用 ComboBoxEdit 控件綁定key/value值:
因為 ComboBoxEdit 沒有 DataSource 屬性,所以不能直接綁定數據源,只能一項一項的添加。
首先創建一個類ListItem:
public class ListItem : Object
{
public string Text { get; set; }
public string Value { get; set; }
public ListItem(string text,string value)
{
this.Text = text;
this.Value = value;
}
public override string ToString()
{
return this.Text;
}
}
//然后把你要綁定的key/value循環添加到ListItem類中:
public void BindSource()
{
string text = string.Empty;
string value = string.Empty;
ListItem item = null;
for (int i = 0; i < 4; i++)
{
if (i==0)
{
text = "請選擇";
}
else
{
text = "選項" + i.ToString();
}
value = i.ToString();
//循環添加
item = new ListItem(text, value);
//將已添加的項綁定到控件的items屬性中,這里只做簡單的演示,可以根據自己的需求添加數據源
this.comboBoxEdit1.Properties.Items.Add(item);
}
}
//獲取選中項的值,其實是醬紫的;
string text = string.Empty;
string value = string.Empty;
if (comboBoxEdit1.SelectedIndex < 0) //小於0,表示未選擇,如果是輸入的也小於0
{
text = comboBoxEdit1.Text.Trim(); //只能獲取輸入的文本
}
else
{
text= (comboBoxEdit1.SelectedItem as ListItem).Text; //獲取選中項文本
value = (comboBoxEdit1.SelectedItem as ListItem).Value; //獲取選中項的值
}
//OK,就是這么簡單。
