欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF下拉框绑定数据

程序员文章站 2022-06-07 17:59:27
...

做WPF项目中,下拉框绑定数据是必不可少的一个功能,这个功能是把存在数据库对应的相关数据取出来,就比如一个颜色的下拉框选项,当你点击这个下拉框的时候就会把你储存在数据库的数据全部查询出来
WPF下拉框绑定数据
1、这个是数据库的存储过程,@Type后面接的是方法名是执行数据查询的根据,根据自己建表的思路把所需要的列查询出来,我下面是从t_detailed_attribute_gather表里根据t_detailed_attribute_gather.attribute_gather_id =7的条件来查询detailed_attribute_gather_id、detailed_attribute_gather_name重命名为Color_id、Color_name,在不触发执行查询的时候,后面拼接了个请选择选方便区分。

IF(@Type='InsertCommodity_Loaded_SelectColor')
SELECT RTRIM(detailed_attribute_gather_id)AS Color_id,
RTRIM(detailed_attribute_gather_name) AS Color_name
FROM      t_detailed_attribute_gather
WHERE t_detailed_attribute_gather.attribute_gather_id =7
union
Select 0,'---请选择---'from t_detailed_attribute_gather
2、这个是在页面绑定下拉框数据的代码,找到对应的下拉框cbo_Color绑定颜色ID和name。
DataTable dtColor = myClient.InsertCommodity_Loaded_SelectColor().Tables[0];
cbo_Color.ItemsSource = dtColor.DefaultView;
cbo_Color.DisplayMemberPath = "Color_name";
cbo_Color.SelectedValuePath = "Color_id";

3、在服务端绑定颜色操作查询,要注意一个方法就需要一个操作契约,服务端的代码和数据库的方法名字要一致不然无法执行下去。

//操作契约
[OperationContract]
private DataSet InsertCommodity_Loaded_SelectColor()
{
  //实例化对象数组(序列化参数)
SqlParameter[] SQLCMDpas =
{
//(定义传递参数,以及传递参数的类型)
new SqlParameter("@type",SqlDbType.Char),
};
//给对象赋值
SQLCMDpas[0].Value = "InsertCommodity_Loaded_SelectColor";
//缓存数据
DataSet myDataTable = myDALMethod.QueryDataSet("UC_Commodity", SQLCMDpas);
return myDataTable;
}