首页 > 代码库 > WPF-ComboBox的数据绑定

WPF-ComboBox的数据绑定

1.示例1

 ObservableCollection<ProductImg> imgs = new ObservableCollection<ProductImg>();   //集合,即数据源         comboBox1.SelectedValuePath = "Id";   //程序内部维护的值         comboBox1.DisplayMemberPath = "Img";  //显示的内容         comboBox1.ItemsSource = imgs;  //数据源         comboBox1.SelectedValue = http://www.mamicode.com/3//选中的值

2.linq 绑定

       //声明数据上下文            DataClasses1DataContext db = new DataClasses1DataContext();            List<TBProduct> pros = new List<TBProduct>();            //查出数据源            var linq = (from p in db.TBProduct                        select p).Take(10);            var linq1 = (from p in db.TBProClassRelation select p).ToList();            //绑定到ComboBox控件中            this.cbProductType.ItemsSource = linq1;            this.cbProductType.SelectedValuePath = "ID";            this.cbProductType.DisplayMemberPath = "Name";