【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
原创粉丝点击