自定义控件 模板控件
来源:互联网 发布:python运维 编辑:程序博客网 时间:2024/05/16 07:49
最近学习自定义控件中的模板控件,下面先贴控件代码和效果图
using System.ComponentModel;using System.Web.UI;using System.Web.UI.Design;using System.Web.UI.WebControls;namespace PZY.BLL.Web.lx._6sjq.mb{ //给控件添加一个设计器 [Designer(typeof(MyTemplateControlDesigner))] public class MyTemplateControl : WebControl, INamingContainer { private ITemplate[] _templates; //声明模板集 public MyTemplateControl() { _templates = new ITemplate[4]; //定义模板集 } //模板1 [Browsable(false),PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate Template1 { get { return _templates[0]; } set { _templates[0] = value; } } //模板2 [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate Template2 { get { return _templates[1]; } set { _templates[1] = value; } } //模板3 [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate Template3 { get { return _templates[2]; } set { _templates[2] = value; } } //模板4 [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate Template4 { get { return _templates[3]; } set { _templates[3] = value; } } //把模板添加到子控件 protected override void CreateChildControls() { for(int i=0;i<4;i++) { Panel pan=new Panel(); _templates[i].InstantiateIn(pan); this.Controls.Add(pan); } } } /// <summary> /// 定义设计器 /// </summary> public class MyTemplateControlDesigner:ControlDesigner { private TemplateGroupCollection col = null; public override void Initialize(IComponent component) { base.Initialize(component); SetViewFlags(ViewFlags.TemplateEditing,true); } //设计时控件显示的内容 public override string GetDesignTimeHtml() { MyTemplateControl control = (MyTemplateControl) Component; string message = "Click here and use the task menu to edit the templates,"; if(control.Template1!=null) { message += "<br/>Template1 finished"; } if (control.Template2 != null) { message += "<br/>Template2 finished"; } if (control.Template3 != null) { message += "<br/>Template3 finished"; } if (control.Template4 != null) { message += "<br/>Template4 finished"; } return CreatePlaceHolderDesignTimeHtml(message); } /// <summary> /// 通过重写TemplateGroups属性为控件每一个模板对应一个模板定义 /// TemplateGroup对模板进行分组 /// TemplateDefinition定义模板(父设计器,模板名字,模板控件,模板控件中的ITemplate属性) /// </summary> public override TemplateGroupCollection TemplateGroups { get { if(col==null) { col = base.TemplateGroups; TemplateDefinition tempDef; MyTemplateControl ctl = (MyTemplateControl) Component; //模板组A TemplateGroup tempGroup=new TemplateGroup("Template Set A"); tempDef=new TemplateDefinition(this,"Template A1",ctl,"Template1",true); tempGroup.AddTemplateDefinition(tempDef); tempDef = new TemplateDefinition(this, "Template A2", ctl, "Template2", true); tempGroup.AddTemplateDefinition(tempDef); col.Add(tempGroup); //模板组B tempGroup = new TemplateGroup("Template Set B"); tempDef = new TemplateDefinition(this, "Template B1", ctl, "Template3", true); tempGroup.AddTemplateDefinition(tempDef); tempDef = new TemplateDefinition(this, "Template B2", ctl, "Template4", true); tempGroup.AddTemplateDefinition(tempDef); col.Add(tempGroup); } return col; } } /// <summary> /// 是否允许改变大小 /// </summary> public override bool AllowResize { get { if(this.InTemplateMode) { return true; } return false; } } }}
分析
要定义一个模板控件需要实现2个部件,控件MyTemplateControl和设计器MyTemplateControlDesigne。
1.MyTemplateControl。
(1)在CreateChildControls中定义了4个ITemplate模板
(2)重写CreateChildControls方法,把ITemplate中的控件添加到Panel,再把Panel添加到子控件集(Controls)中。
<ITemplate>
childrenControl
.........
childrenControl
</ITemplate>
变为
<panel>
childrenControl
.........
childrenControl
</panel>
2.设计器MyTemplateControlDesigner
(1)Initialize(container),传入一个容器控件,把子控件添加到容器中
(2)GetDesignTimeHtml():返回设计时控件显示的代码
(3)TemplateGroups:通过重写TemplateGroups属性为控件每一个模板对应一个模板定义
(4)AllowResize:是否允许拖动改变大小
3.在MyTemplateControl上方添加代码[Designer(typeof(MyTemplateControlDesigner))]指定设计器
阅读全文
1 0
- 自定义控件 模板控件
- 自定义用户控件模板
- 自定义控件(支持模板)
- 自定义控件(支持模板)
- 自定义组合控件--自定义标题栏模板
- 自定义控件(模板+数据绑定)
- 自定义控件在DATAGRID模板中的应用
- ASP.NET数据控件的自定义模板
- [转] 使用模板自定义 WPF 控件
- [转] 使用模板自定义 WPF 控件
- Balsamiq Mockups 自定义控件和页面模板
- 自定义控件之定义UI模板
- WPF 构建自定义控件 模板Template
- Silverlight 自定义控件 模板化控件 (一)属性
- Silverlight 自定义控件 模板化控件 (二)事件
- 模板控件
- 自定义控件----倒计时控件
- 自定义控件-组合控件
- 八百里加急$数据结构追加1用户权限库表设计
- Android 屏幕适配攻略(三)单位dp与sp
- js中通过Object.prototype.toString方法----精判断对象的类型
- linux环境下 java nio 文件名 乱码 utf-8 gbk
- java练习——实现系统入口程序
- 自定义控件 模板控件
- mysql 数据库函数
- 低于1.0版本 pod使用
- 3DES加解密-ZipCompressor
- 图片轮播
- N
- 0-1背包问题
- Androidstudio 导入mode 报错:"Manifest merger failed with multiple errors, see logs"
- mybatis查询时间段sql语句