【WPF】给下拉列表ComboBox绑定数据
思路:给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绑定数据的更多相关文章
- ListBox和ComboBox绑定数据简单例子
1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...
- Winfrom 中 ComboBox 绑定数据后设置选定项问题
在为 ComboBox 当定数据的时候,如果遇到界面显示需要用文本,而获取选定项的值时需要用数字,我们就很习惯使用 DataSource 来进行绑定. 例如以下代码: List<TextVal ...
- (摘)C#comboBox绑定数据
C#中comboBox用代码绑定数据库中在某一列.用处:跟radioButton联系在一起,可以根据radioButton在选择而在comboBox显示出不同的值. private void radi ...
- winform combobox绑定数据
mboBox下拉菜单控件,在数据库内的ComboBox应用的表进行修改时,如果是用的普通方法,显示数据一个方法,添加数据一个方法 这样会导致程序后期维护难度增加,在这里使用数据绑定来让ComboBox ...
- 学习日记10、easyui编辑器combobox绑定数据的两种方式
1.数据本地绑定 var card = [{ "value": "正常", "text": "正常" }, { &quo ...
- 也谈解决Combobox绑定数据后取值出现System.Data.DataRowView的问题
刚才遇到一个怪现象:同一个窗口,同一张表,通过第一个Combobox值的改变,动态绑定第二个Combobox,结果出现一个怪现象,第一个Combobox有的值改变第二个Combobox一切正常,有几个 ...
- winform中的ListBox和ComboBox绑定数据
将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...
- 【WPF】两个下拉列表ComboBox的级联
需求:两个ComboBox的级联,实现城市–小区级联. 问题:个人感觉WPF的核心应该是数据绑定这块.由于时间紧迫,粗略看Binding也是一头雾水,所以用了比较简单的方法做了两个下拉列表级联的效果: ...
- 【WPF】绑定数据
WPF绑定数据 模型类(继承 INotifyPropertyChanged,实现属性的变更通知)
随机推荐
- hdu 4412 Sky Soldiers(区间DP)
Sky Soldiers Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- linux配置server笔记
设置防火墙开放80port -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 尽管看不懂是什么,可是这个是用于开放80p ...
- android.webkit.WebView/WebViewClient/WebChromeClient
使用android.webkit.WebView控件 在xml布局文件中定义 <WebView android:id="@+id/webkit01" android: ...
- Spring配置文件头信息
代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- HDUOJ ---1269迷宫城堡
http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 运行shell出错: 没有那个文件或目录
http://blog.163.com/zhangjie_0303/blog/static/99082706201136114548840/
- js常用点
<span onClick="onDetail(this.id,'edit');" style="cursor: hand;color:black;" ...
- python练习笔记——map | sum | pow 的应用
1 函数简要 map 函数 | sum 函数 | pow函数 | lambda函数 2 简要计算 2.1 1^2 + 2^2 + 3^2 .....9^2 方法1 print([pow(x,2 ...