赞
踩
ComboBox 绑定 DataTable 数据
- DeviceTypeViewModel deviceTypeModel = new DeviceTypeViewModel();
- this.parentIdTxt.ItemsSource = deviceTypeModel.GetDeviceType().DefaultView;
- this.parentIdTxt.DisplayMemberPath = "name";
- this.parentIdTxt.SelectedValuePath = "id";
但我们经常会在诸如分类时添加顶级分类选项,所以我是这样做的,先把数据加入Dictionary,在使用Dictionary作为数据源
- public Dictionary<int, string> ComboboxItem()
- {
- DataTable deviceTypes = GetDeviceType();
- Dictionary<int, string> items = new Dictionary<int, string>();
- items.Add(0, "顶级分类");
- foreach (DataRow row in deviceTypes.Rows)
- {
- items.Add(Convert.ToInt32(row["id"]), row["name"].ToString());
- }
- return items;
- }
- DeviceTypeViewModel deviceTypeModel = new DeviceTypeViewModel();
- Dictionary<int, string> items = deviceTypeModel.ComboboxItem();
- this.parentIdTxt.ItemsSource = items;
- this.parentIdTxt.DisplayMemberPath = "Value";
- this.parentIdTxt.SelectedValuePath = "Key";
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。