VS環境下,DEV插件的ComboBoxEdit控件最簡單的數據源綁定和獲取方法


使用 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,就是這么簡單。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM