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的部分。
/////////////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对方法对接。
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显示。
/// <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动态指定选取节点。
本文示例项目知识点: