思路:给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;
}

【WPF】给下拉列表ComboBox绑定数据的更多相关文章

  1. ListBox和ComboBox绑定数据简单例子

    1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...

  2. Winfrom 中 ComboBox 绑定数据后设置选定项问题

    在为 ComboBox 当定数据的时候,如果遇到界面显示需要用文本,而获取选定项的值时需要用数字,我们就很习惯使用 DataSource  来进行绑定. 例如以下代码: List<TextVal ...

  3. (摘)C#comboBox绑定数据

    C#中comboBox用代码绑定数据库中在某一列.用处:跟radioButton联系在一起,可以根据radioButton在选择而在comboBox显示出不同的值. private void radi ...

  4. winform combobox绑定数据

    mboBox下拉菜单控件,在数据库内的ComboBox应用的表进行修改时,如果是用的普通方法,显示数据一个方法,添加数据一个方法 这样会导致程序后期维护难度增加,在这里使用数据绑定来让ComboBox ...

  5. 学习日记10、easyui编辑器combobox绑定数据的两种方式

    1.数据本地绑定 var card = [{ "value": "正常", "text": "正常" }, { &quo ...

  6. 也谈解决Combobox绑定数据后取值出现System.Data.DataRowView的问题

    刚才遇到一个怪现象:同一个窗口,同一张表,通过第一个Combobox值的改变,动态绑定第二个Combobox,结果出现一个怪现象,第一个Combobox有的值改变第二个Combobox一切正常,有几个 ...

  7. winform中的ListBox和ComboBox绑定数据

    将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...

  8. 【WPF】两个下拉列表ComboBox的级联

    需求:两个ComboBox的级联,实现城市–小区级联. 问题:个人感觉WPF的核心应该是数据绑定这块.由于时间紧迫,粗略看Binding也是一头雾水,所以用了比较简单的方法做了两个下拉列表级联的效果: ...

  9. 【WPF】绑定数据

    WPF绑定数据 模型类(继承 INotifyPropertyChanged,实现属性的变更通知)

随机推荐

  1. mybatis对mysql进行分页

    Mybatis对mysql数据库分页 在generator中增加插件,下载地址http://download.csdn.net/detail/shunlongjin/6937045 <plugi ...

  2. PHP-Ajax跨域解决方案

    1.先了解下Ajax跨域问题: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...

  3. Dell笔记本Ubuntu无线网卡驱动安装

    [日期:2010-06-09]   经过长时间学习Ubuntu无线网卡,,你可能会遇到Ubuntu无线网卡问题,,这里将介绍Ubuntu无线网卡问题的解决方法整了一晚上,终于在我的dell 1440上 ...

  4. 去掉cb中括号的匹配

    Settings->Editor->General settings->Indent options->Brace completion``

  5. 如何用python的装饰器定义一个像C++一样的强类型函数

        Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...

  6. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. jQuery动态网格瀑布流插件Masonry

    Masonry是一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发瀑布流界面效果.和CSS中float的效果不太一样的地方在于,float先水平排列,然后再垂直排列,使用Masonr ...

  8. php 文件上传,下载

    文件下载: html: <html> <body> <a href="1.rar">下载1.rar</a> <br /> ...

  9. 结构体位制 中存在 有符号 与 无符号 -- C

    #include <stdio.h> #include <stdlib.h> #include <string.h> /* 有符号 结构体1 */ struct b ...

  10. bug list

    机型: Samsung Galaxy S GT-I9000 版本: 2.2.1bug: Couldn't create directory for SharedPreferences file xxx ...