zoukankan      html  css  js  c++  java
  • 星空雅梦

    WPF】给下拉列表ComboBox绑定数据

     版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18995513/article/details/54585895

    思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是该实体类的某一属性(如person.Name)。

    大致步骤:

    • 联网获取到这组数据的Json,然后反序列化为对应的List< 实体类 >列表。
    • 由于只想要绑定这组实体类的Name属性,所以再准备一个List< string >集合,保存List< 实体类 >中的每一个对象的Name属性
    • 最后ComboBox的ItemSource绑定到这个List< string >集合即可。

    前台绑定:

    <ComboBox ItemsSource="{Binding CityName}"/>
    • 1

    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; }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    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);
        }
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    联网工具类:

    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;
    }
  • 相关阅读:
    F广搜
    Python中range和xrange的异同之处
    数组中出现次数超过一半的数字
    iOS开发之剖析&quot;秘密&quot;App内容页面效果(一)
    Balloon Comes!
    scikit-learn: isotonic regression(保序回归,非常有意思,仅做知识点了解,但差点儿没用到过)
    C#数据缓存介绍及Caching通用帮助类整理
    SVN Working copying &#39;xxxxx/xxxx/xxxx&#39; locked
    读书笔记-APUE第三版-(7)进程环境
    UVA 10555
  • 原文地址:https://www.cnblogs.com/LiZhongZhongY/p/11066891.html
Copyright © 2011-2022 走看看