wpf combobox 的使用

IList<his_nation> customList = new List<his_nation>();
        public Window1()
        {
            InitializeComponent();
            BindCombobox();
        }

初始化


        public void BindCombobox()
        {
            IList<String> NationstrList = new List<String>();


            NationstrList.Add("汉族");
            NationstrList.Add("壮族");
            NationstrList.Add("Cat");


            String str = NationstrList[0];




            customList.Add(new nation() { ID = 1, Name = "汉族" });
            customList.Add(new nation() { ID = 2, Name = "壮族" });
            customList.Add(new nation() { ID = 3, Name = "Cat" });
            cbb_nation.ItemsSource = customList;
            cbb_nation.DisplayMemberPath = "Name";
            cbb_nation.SelectedValuePath = "ID";
            cbb_nation.SelectedValue = 1;
        }
	public class his_nation
	{
  	  public int ID { 
   	     get; 
     	   set;
  	  }
 	   public string Name { 
   	     get; 
   	     set;
  	  }
	}


获取combobox选中的值

          int tt =  cbb_nation.SelectedIndex;
          String name =  customList[tt].Name;



你可能感兴趣的:(wpf combobox 的使用)