什么是用户控件和自定义控件
用户控件:简单来说就是可以把WPF的控件将多个组合到一起弄成一个控件。当然可以写代码了。也可以给这个组合而成的控件加方法和字段。
特点:不能使用样式和模板,继承UserControl类。但是这个控件组的里面的控件的值得获取和属性的改变都需要自己编写好。
自定义控件:(没有尝试过,参考网上)
可以在现有的控件上面进行扩展,增加新的属性和方法。
有一个代码文件和一个默认的主题文件。
可以使用样式和模板
构建控件库的好方法
为什么要使用用户控件?
额,因为如果使用模板方法,WPF的窗体的继承窗体会总是报错。而且被继承的窗体不能有xmal文件。也就是不能有控件。所有我就使用用户控件代替模板方法。
组合查询之用户控件+存储过程
第一步就是要建立存储过程
在SQL server里面建立组合查询:
注意点:组合查询的后面的两组可能为空值,所有要建立成可以为Null的类型。
里面的char(39)是ASCII里面的单引号,char(32)是空格
create procedure PROC_GroupFindCheck
@Filed1 nvarchar(10),
@Filed2 nvarchar(10)=NULL,
@Filed3 nvarchar(10)=NULL,
@Mark1 nvarchar(2),
@Mark2 nvarchar(2)=NULL,
@Mark3 nvarchar(2)=NULL,
@Content1 nvarchar(30),
@Content2 nvarchar(30)=NULL,
@Content3 nvarchar(30)=NULL,
@Relationship1 nvarchar(5)=NULL,
@Relationship2 nvarchar(5)=NULL,
@DbtableName nvarchar(20)
as
declare @TempSql nvarchar(500)
BEGIN
SET @TempSql='SELECT * FROM '+@DbtableName+' WHERE '+@Filed1+@Mark1+char(39)+@Content1+char(39)
if(@Relationship1!=NULL)
BEGIN
SET @TempSql=@TempSql+@Relationship1+char(32)+@Filed2+@Mark2+char(39)+@Content2+char(39)
if(@Relationship2!=NULL)
BEGIN
SET @TempSql=@TempSql+@Relationship2+char(32)+@Filed3+@Mark3+char(39)+@Content3+char(39)
END
END
ExECUTE(@TempSql)
END
第二步:建立实体层,以及D层,外观层,工厂层,IDAL层,B层。
其实这是我就是说一下DAL层的方法,其他都很简单。
实体层中建立快速属性的方法:输入prop,双击tab键再修改即可。
在D层中,我无法直接返回list,只能这个存储过程是对两个表进行查询的,无法确定返回时哪个表的,我就使用里out穿出两个表的list。
为什么非要返回list类型呢?
因为在WPF当中,是使用空间DataGrid来显示查询的数据的,而他能接受显示的只有list,不能是datatable。
public class GroupFindTemplateDAL:IDAL.IGroupFindTemplateIDAL
{
SQLHelper sqlhelper = new SQLHelper();
public DataTable GroupFindTemplate(Entity.GroupFindTemplateEntity groupfind,out List<Entity.RechargeEntity> rechargelist,out List<Entity.CancelCardEntity> cancelcardlist)
{
SqlParameter[] sqlparas =
{
new SqlParameter("@Filed1",groupfind.Field1),
new SqlParameter("@Filed2",groupfind.Field2),
new SqlParameter("@Filed3",groupfind.Field3),
new SqlParameter("@Mark1",groupfind.Mark1),
new SqlParameter("@Mark2",groupfind.Mark2),
new SqlParameter("@Mark3",groupfind.Mark3),
new SqlParameter("@Content1",groupfind.Content1),
new SqlParameter("@Content2",groupfind.Content2),
new SqlParameter("@Content3",groupfind.Content3),
new SqlParameter("@Relationship1",groupfind.Relationship1),
new SqlParameter("@Relationship2",groupfind.Relationship2),
new SqlParameter("@Dbtablename",groupfind.DbtableName)
};
string sqlstring = @"PROC_GroupFindCheck";//这里是存储过程的名称
DataTable table = sqlhelper.ExecuteQuery(sqlstring, sqlparas, CommandType.StoredProcedure);//直接设置类型为存储过程
rechargelist = null;
cancelcardlist = null;
if (groupfind.DbtableName=="CancelCard")
{
cancelcardlist = ConvertListHelper.convertToList<Entity.CancelCardEntity>(table);
}
else
{
rechargelist = ConvertListHelper.convertToList<Entity.RechargeEntity>(table);
}
return table;
}
}
第三步:建立用户控件并编写代码:
来看一下组件实现的效果:
其实也就是我们组合查询实现的效果。其中查询按钮和到处excel表都不是我的用户控件里面的。其他的都是。
而其中控件的方法都是我在组件里面写好的,因为当你的用户控件创建好之后,就相当于所有的控件合起来才是一个,无法在使用组件的窗体里面使用用户控件里面单个控件的事件和方法。
其中用户控件比较重要的代码就是:我给用户控件添加了一个属性,来获取要查询的表名。还有一个方法,将查询所建立的实体赋值。
//属性
public string GetDbTableName { get; set; }
//给实体赋值的方法
public bool GetSql(out Entity.GroupFindTemplateEntity groupfindentity)
{
groupfindentity= new Entity.GroupFindTemplateEntity();
bool list = true ;
//判断是否为空
int j = 0;
Empty.Em(grid, ref j);
if (j != 0)
{
list = false;
}
else
{
try
{
#region 时间控件使用时,将值赋过去
if (DateContext1.Visibility == Visibility.Visible)
{
txtContent1.Text = DateContext1.Text;
}
if (DateContext2.Visibility == Visibility.Visible)
{
txtContent2.Text = DateContext2.Text;
}
if (DateContext3.Visibility == Visibility.Visible)
{
txtContent3.Text = DateContext3.Text;
}
#endregion
#region 给实体赋值
groupfindentity.DbtableName = GetDbTableName;
groupfindentity.Field1 = GetEnglishFromFiled(cboField1);
groupfindentity.Field2 = GetEnglishFromFiled(cboField2);
groupfindentity.Field3 = GetEnglishFromFiled(cboField3);
groupfindentity.Mark1 = cboMark1.Text;
groupfindentity.Mark2 = cboMark2.Text;
groupfindentity.Mark3 = cboMark3.Text;
groupfindentity.Content1 = txtContent1.Text;
groupfindentity.Content2 = txtContent2.Text;
groupfindentity.Content3 = txtContent3.Text;
groupfindentity.Relationship1 = GetEnglishFromFiled(cboCombination1);
groupfindentity.Relationship2 = GetEnglishFromFiled(cboCombination2);
#endregion
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return list;
}