【WPF】给下拉列表ComboBox绑定数据
2017-01-17 19:16
519 查看
思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是该实体类的某一属性(如person.Name)。
大致步骤:
联网获取到这组数据的Json,然后反序列化为对应的List< 实体类 >列表。
由于只想要绑定这组实体类的Name属性,所以再准备一个List< string >集合,保存List< 实体类 >中的每一个对象的Name属性
最后ComboBox的ItemSource绑定到这个List< string >集合即可。
前台绑定:
ViewModel:
Controller层:
联网工具类:
大致步骤:
联网获取到这组数据的Json,然后反序列化为对应的List< 实体类 >列表。
由于只想要绑定这组实体类的Name属性,所以再准备一个List< string >集合,保存List< 实体类 >中的每一个对象的Name属性
最后ComboBox的ItemSource绑定到这个List< string >集合即可。
前台绑定:
<ComboBox ItemsSource="{Binding CityName}"/>
ViewModel:
private List<City> cityList; // 当前省份下所有城市的信息 public List<City> CityList { get { return cityList; } set { SetProperty(ref cityList, value); } } private List<string> cityName; // 前台下拉列表绑定当前省份下所有城市名 public List<string> CityName { get { return cityName; } set { SetProperty(ref cityName, value); } } // 记得在ViewModel的构造函数中初始化这两个List列表 // ... InitList()... public class City { public int cityId { get; set; } public string cityName { get; set; } }
Controller层:
// 联网获取城市/小区Json数据 private void GetCityAndCommunityJsonData() { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("provinceName", "广西壮族自治区"); // 暂时写死 string request = GlobalVariable.GET_CITYS_BY_PROVINCE_TO_CLIENT; string json = NetworkUtils.Instance.HttpPostRequest(request, dic); System.Console.WriteLine("完成:获取城市/小区数据"); Response<City> response = JsonConvert.DeserializeObject<Response<City>>(json); houseTypeViewModel.CityList.Clear(); houseTypeViewModel.CityList = response.result; houseTypeViewModel.CityName.Clear(); foreach (var item in houseTypeViewModel.CityList) { houseTypeViewModel.CityName.Add(item.cityName); } }
联网工具类:
public string HttpPostRequest(string url, IDictionary<string, string> parameters) { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GlobalVariable.SERVER_ADDRESS_TEMP + url); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf8"; httpWebRequest.Timeout = 20000; // 参数 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); } i++; } // 给文本数据编码 byte[] data = Encoding.UTF8.GetBytes(buffer.ToString()); // 往请求的流里写数据 using (Stream stream = httpWebRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } // 从响应对象中获取数据 HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string responseContent = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); httpWebRequest.Abort(); return responseContent; }
相关文章推荐
- ASPxComboBox下拉列表数据绑定
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- # winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- C#_WinForm的ComboBox与数据绑定详解(关于Text属性和下拉列表)
- c# winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)
- 转:c# winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- Ext中下拉列表ComboBox组件store数据格式用法介绍
- VC中点击ComboBox下拉列表不显示数据的解决方法
- VB.Net程序设计:ComboBox下拉列表中显示多列数据-增加一个事件。
- ExtJs——ComboBox(下拉列表、从后台读取数据、带分页)!!!
- VC中点击ComboBox下拉列表不显示数据的解决方法
- Ext comboBoxTree (完整的数据加载)--下拉列表里边是树结构
- wpf combobox数据绑定并添加一个额外的选项
- 实现树形下拉列表 GridView数据绑定
- RadComboBox 的 Virtualization 方式,解决数据过多,下拉列表显示缓慢问题。
- 下拉列表无级数据绑定
- [转]VC中ComboBox下拉列表不显示数据或下拉表高度太小的问题
- 三级下拉列表的修改时无法绑定原始数据解决办法
- wpf datagrid中的 DataGridComboBoxColumn数据绑定