【WPF】给下拉列表ComboBox绑定数据
来源:互联网 发布:电影后期特效制作软件 编辑:程序博客网 时间:2024/05/17 21:19
思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是该实体类的某一属性(如person.Name)。
大致步骤:
- 联网获取到这组数据的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;}
0 0
- 【WPF】给下拉列表ComboBox绑定数据
- WPF 数据绑定 combobox
- WPF MVVM 绑定ComboBox数据
- ComboBox下拉框数据绑定
- # winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- 【WPF】两个下拉列表ComboBox的级联
- 下拉列表数据绑定
- ComboBox 下拉列表控件绑定值
- WPF学习笔记:ComboBox的数据绑定
- wpf ComboBox 控件利用DataSet数据绑定
- Combobox下拉框绑定数据时,注意事项!
- 下拉列表无级数据绑定
- 下拉列表无级数据绑定
- 【WPF】获取下拉列表(ComboBox)的选项(ComboBoxItem)的内容
- wpf:combobox绑定数据库
- wpf combobox绑定dictionary
- combobox下拉列表控件 关联绑定两个表的技巧
- VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)
- Redis---String数据类型的操作
- ubuntu 与windows双系统记录
- codeforce B. Bash's Big Day
- LightOJ 1236 Pairs Forming LCM(唯一分解定理)
- iOS手势与点击事件冲突
- 【WPF】给下拉列表ComboBox绑定数据
- 【Unity问题】为什么Unity 5.5.0f3(64-bit)免费版打开项目每次要输入账号密码
- Ubuntu下git的使用
- React Native升级指南|v0.40+升级适配经验与心得
- arm编译安装openCV整理
- Docker#docker dead but pid file exists
- 使用Hexo+Github+Ubuntu搭建博客
- python异常处理流程
- 3D游戏引擎底层数据结构的封装之Dictionary