zoukankan      html  css  js  c++  java
  • wpf之ComboBox绑定

        

        方式一、通过泛型Dictionary绑定

    View Code
               Dictionary<intstring> mydic = new Dictionary<intstring>() { 
                {0,"aaaa"},
                {1,"bbbb"},
                {2,"cccc"}
                };
                cobxUserType.ItemsSource = mydic;
                cobxUserType.SelectedValuePath = "Key";
                cobxUserType.DisplayMemberPath = "Value";

      然后通过cobxUserType.SelectedValue获得选中的值即可

      方法二、通过数据集绑定

        

    View Code
      WebDictClassServices.WebDictClass webDict = new WebDictClass();
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                BindCombox();
                BindCombox2(comboBox2,0,"选择系列");
                BindCombox2(comboBox3,0,"选择型号");
            }
            //绑定下拉控件1
            private void BindCombox()
            {
                DataTable dt = webDict.GetMainBrand();
                if (dt != null && dt.Rows.Count == 0)
                {
                    dt = new DataTable();
                    DataColumn dc1 = new DataColumn("Title");
                    DataColumn dc2 = new DataColumn("ID");
                    dt.Columns.Add(dc1);
                    dt.Columns.Add(dc2);
                    DataRow row = dt.NewRow();
                    row["title"] = "按品牌选择";
                    row["ID"] = "0";
                    dt.Rows.Add(row);
                }
                else
                {
                    DataRow row = dt.NewRow();
                    row["title"] = "按品牌选择";
                    row["ID"] = "0";
                    dt.Rows.Add(row);
                }
                DataView dv=dt.DefaultView;
                dv.Sort=" ID asc";
                combMainBrand.ItemsSource =dv;
                this.combMainBrand.DisplayMemberPath = "Title";
                this.combMainBrand.SelectedValuePath = "ID";
                combMainBrand.SelectedValue = "0";

            }
            //绑定下拉控件
            private void BindCombox2(ComboBox comb, int id,string message)
            {
                DataTable dt = null;

                if (id == 0)
                {
                    dt = new DataTable();
                    DataColumn dc1 = new DataColumn("Title");
                    DataColumn dc2 = new DataColumn("ID");
                    dt.Columns.Add(dc1);
                    dt.Columns.Add(dc2);
                    DataRow row = dt.NewRow();
                    row["Title"] = message;
                    row["ID"] = "0";
                    dt.Rows.Add(row);
                }
                else
                {
                    dt = webDict.GetCarSeriesByBrand(id);
                    DataRow row = dt.NewRow();
                    row["Title"] = message;
                    row["ID"] = "0";
                    dt.Rows.Add(row);
                }
                DataView dv = dt.DefaultView;
                dv.Sort = " ID asc";
                comb.ItemsSource = dv;
                comb.DisplayMemberPath = "Title";
                comb.SelectedValuePath = "ID";
                if (dt.Rows.Count!=0)
                {
                  comb.SelectedValue = dt.Rows[0]["ID"];
                }
            }

            private void combMainBrand_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int id = Convert.ToInt32(combMainBrand.SelectedValue);
                BindCombox2(comboBox2, id, "选择系列");
                BindCombox2(comboBox3, Convert.ToInt32(comboBox2.SelectedValue), "选择型号");
            }

            private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int id = Convert.ToInt32(comboBox2.SelectedValue);
                BindCombox2(comboBox3, id, "选择型号");
            }

       三、IList绑定(类似winForm)

      

    View Code
      public class Info
        {
            public int Id { getset; }
            public string Name { getset; }

        }
            //绑定用户类型
            private void bindUserType()
            {
                IList<Info> list = new List<Info>() { 
                 new Info(){ Id=0, Name="供应商"}, new Info(){ 
                  Id=1, Name="bbbbbb"
                 
                };
                cobxUserType.ItemsSource = list;
                cobxUserType.SelectedValuePath = "Id";
                cobxUserType.DisplayMemberPath = "Name";


            }

           

  • 相关阅读:
    python核心编程(多线程编程)
    Python核心编程(网络编程)
    将非drf接口配置到swagger
    jmeter设置全局变量--通过正则表达式进行提取
    jmeter实现用户登录高并发
    Django跨关联关系查询
    python树状结构取值和加值
    chrome浏览器代理插件SwitchyOmega使用
    burp suite历程-安装burp suite
    django中对模型字段名的限制
  • 原文地址:https://www.cnblogs.com/shuang121/p/2788991.html
Copyright © 2011-2022 走看看