首页 > 下载 > 下载详文:WPF的ComboBox关联数据绑定

WPF的ComboBox关联数据绑定

发布时间:2012年03月22日 14时55分31秒   属性:程序Web开发 > Microsoft    访问次数:85803
字体: 初始 添加收藏 分享给好友

WPF的ComboBox关联数据绑定

WPF的ComboxBox绑定关联数据,多个ComboBox关联绑定数据。本文实现两个ComboBox的关联数据绑定,继续 WPF的TreeView从数据库绑定数据 文中的数据源,在这里用以关联ComboBox的数据绑定,同时在其数据操作类上进程拓展。在CityService数据服务端程序,本文示例中加入了一个WCF服务CityWCFService,演示ComboBox是通过WCF提供数据绑定,同时也适用于Silverlight 的数据绑定。下面是程序执行效果。

数据库部分

在本文中用到两个表,分别是CityData(一级城市列表)、CityItemData(城市子级分区)对应两个ComboBox的关联绑定。在数据库查询CityData加入了一条查询 SELECT CityId, CityName FROM CityData WHERE (CityId = @CityId)即GetDataByCityId(@CityId)方法,根据城市Id查询城市,其它保持不变。

在ControlData类中将CityData的GetData()方法和GetDataByCityId(@CityId)包装成List泛型数据列表。MasterCity(bool isHeader)为包装GetData()方法,带入bool型参数isHeader,如果为True则有默认修饰选取值,False则没有。private string MasterCityById(int id)则为包装GetDataByCityId(@CityId),用MasterCityById(int id)通过代入的城市Id返回城市名。然后CityItem(int id)则包装CityItemData的GetDataById(@CityId),通过选取城市带入入城市Id获取所属城市的分区。下面是ControlData类2012.03.22的部分。

C# 代码  复制
/////////////Copyright (C) 遗昕 | weisim3.com 03.22.2012////////////////////////// /////////////WPF的ComboBox关联数据绑定///////////////////////////////////////////// /// <summary> /// MasterCity -> 获取主城区 /// </summary> /// <param name="isHeader">isHeader ->是否需要头标签(true需要,false否)</param> /// <returns></returns> public List<CityInfo> MasterCity(bool isHeader) { List<CityInfo> list = new List<CityInfo>(); if (isHeader) { list.Add(new CityInfo() { CityId = 0, CityName = "-选取城市-" }); } foreach (DataRow row in CityData.GetData()) { CityInfo myCity = new CityInfo(); myCity.CityId = Convert.ToInt32(row["CityId"]); myCity.CityName = row["CityName"].ToString(); list.Add(myCity); } return list; } /// <summary> /// MasterCityById ->根据城市Id返回城市 /// </summary> /// <param name="id">CityId</param> /// <returns></returns> private string MasterCityById(int id) { string CityName = string.Empty; foreach (DataRow row in CityData.GetDataByCityId(id)) { CityName = row["CityName"].ToString(); } return CityName; } /// <summary> /// CityItem ->城市分区集合 /// </summary> /// <param name="id">CityId</param> /// <returns></returns> public List<CityInfo> CityItem(int id) { List<CityInfo> list = new List<CityInfo>(); list.Add(new CityInfo() { CityItmeId = 0, CityItmeName = MasterCityById(id) }); foreach (DataRow itme in CityDataItem(id)) { CityInfo myCity = new CityInfo(); myCity.CityItmeId = Convert.ToInt32(itme["CityItemId"]); myCity.CityItmeName = itme["CityItemName"].ToString(); list.Add(myCity); } return list; }

在CityService中建立WCF服务在本示例中命名为CityWCFService,CityWCFService继承了ICityWCFService,再 将List<CityInfo> CityItem(int id)和List<CityInfo> MasterCity(bool isHeader)包装在CityWCFService服务类中,在ICityWCFService中实现CityWCFService对方法对接。

C# 代码  复制
public class CityWCFService : ICityWCFService { public string DoWork() { return "Hello World!!"; } public List<CityInfo> CityItem(int id) { return new ControlData().CityItem(id); } public List<CityInfo> MasterCity(bool isHeader) { return new ControlData().MasterCity(isHeader); } } [ServiceContract] public interface ICityWCFService { [OperationContract] string DoWork(); [OperationContract] List<CityInfo> CityItem(int id); [OperationContract] List<CityInfo> MasterCity(bool isHeader); }

WPF引用WCF数据绑定ComboBox

WPF项目TreeViewTest中添加服务器引用,在当前项目中发现即“http://localhost:5985/CityWCFService.svc”,可以发现CityWCFService.svc和Service1.asmx,在本示例中用的是WCF直接选取CityWCFService.svc,依次展开可以看见对应的方法。如果对WCF不熟悉可以参考 Silverlight与WCF服务 。

WCF服务成功引入之后,接着在TreeViewTest中新建一个WPF窗体,在本示例中命名为weisim3ComboBox,在窗体中放入两个ComboBox和一个Label,转入后台weisim3ComboBox.xaml.cs中引入using TreeViewTest.ServiceReference1;,实例化CityWCFService, CityWCFServiceClient cityService = new CityWCFServiceClient();,在Window_Loaded中将cityService.MasterCity(true)绑定赋给ComboBox1.ItemsSource,指定ComboBox的DisplayMemberPath为“CityName”,SelectedValuePath为CityItmeId。在comboBox1_SelectionChanged中绑定ComboBox2的ItemsSource,如果ComboBox1的默认选取值为0,ComboBox则绑定临时数据list,同时指定DisplayMemberPath为CityItmeName,SelectedValuePath为CityItmeId,comboBox2.SelectedValue为0,这里是默认选取;否则让ComboBox2绑定cityService.CityItem(Convert.ToInt32(comboBox1.SelectedValue))与ComboBox1的SelectedValue值关联。最后在comboBox2_SelectionChanged中将ComboBox1和ComboBox2的值赋给Label显示。

C# 代码  复制
/// <summary> /// weisim3ComboBox.xaml /// WPF的ComboBox关联数据绑定 /// Copyright (C) 遗昕 | weisim3.com 03.22.2012 /// </summary> public partial class weisim3ComboBox : Window { CityWCFServiceClient cityService = new CityWCFServiceClient(); public weisim3ComboBox() { InitializeComponent(); } private void comboBox1_SelectionChanged(object sender,
SelectionChangedEventArgs e) { if (Convert.ToInt32(comboBox1.SelectedValue) == 0) { List<CityInfo> list = new List<CityInfo>(); list.Add(new CityInfo() { CityItmeId = 0, CityItmeName = "-城市分区-" }); comboBox2.ItemsSource = list; comboBox2.DisplayMemberPath = "CityItmeName"; comboBox2.SelectedValuePath = "CityItmeId"; comboBox2.SelectedValue = 0; //comboBox2.Items.Add("-城市分区-"); //comboBox2.SelectedValue = "-城市分区-"; } else { comboBox2.ItemsSource =
cityService.CityItem(Convert.ToInt32(comboBox1.SelectedValue)); comboBox2.DisplayMemberPath = "CityItmeName"; comboBox2.SelectedValuePath = "CityItmeId"; comboBox2.SelectedValue = 0; } } private void Window_Loaded(object sender, RoutedEventArgs e) { comboBox1.ItemsSource = cityService.MasterCity(true); comboBox1.DisplayMemberPath = "CityName"; comboBox1.SelectedValuePath = "CityId"; comboBox1.SelectedValue = 0; } private void comboBox2_SelectionChanged(object sender,
SelectionChangedEventArgs e) { try { label1.Content = ((CityInfo)comboBox1.SelectedItem).CityName + ">>>" + ((CityInfo)comboBox2.SelectedItem).CityItmeName; } catch { } } }

总结本文示例,文章示例和WPF的TreeView从数据库绑定数据 基本相同,只是在传输数据是采用了WCF作为服务端服务,而之前是才采的Webservice作为服务端服务,Webservice和WCF两者有何不同,Webservice只要在.NET Framework 2.0以上就能支持,WCF需要3.0以上版本才可支持。WCF的功能要比Webservice强大,安全密封性更高。而Webservice如果服务器机器只能支持.NET Framework 2.0 则只能Webservice,当然Webservice安全性能也是非常好,只是相对以3.0上的WCF要有所落差。通过这个两个示例的TreeView和ComboBox数据绑定都可以看到它传输到客户端的数据应用操作方式都没有多少差异。最后,提示先将服务段程序启动再启动编译运行TreeView项目,只需把CityService中的WebForm1.aspx启用预览即可,即启动了Websrvcie和WCF的服务。关于TreeView节点动态控制可以参考WPF TreeView动态指定选取节点

本文示例项目知识点:

  • 数据库数据表建立
  • DataSet数据集结合存储过程查询使用
  • WCF服务端程序使用,可以参考(Silverlight与WCF服务
  • WPF程序,即是本文的中心ComboBox绑定数据库数
WPF的ComboBox关联数据绑定 (22)
本下载连接不支持第三下载工具打开,请直接点击下载即可
文章版权归属weisim3.com所有,未经书面版权许可同意,不得私自转载(或做修改转载),源文件示例仅供学习使用,更不要出于商业用途或印刷出版发行!否则将追究其相关法律责任,版权联系QQ:729260499。
遺昕 | Weisim3.com 下载许可条款 ( 您本次需要付费下载 ) .



付费源文件: WPF的ComboBox关联数据绑定
支付金额: ¥30.00        授权期限: 10
eU0dJUlkVnDyK3LeH22gB8UiwHDo13 -- 20240329164757