ASP.NET控件设计时支持之自动格式设置浅析

开发 后端
ASP.NET控件设计时支持之自动格式设置就是向大家介绍ASP.NET控件设计时支持之自动格式设置的相关实现内容。

ASP.NET控件设计时支持之自动格式设置是如何实现的呢?

先看个图

自动格式设置 

相信大家都很熟悉吧,我们可以用这个面板很方面的使用预定的样式.我们可以称之为自动格式设置或者自动套用样式.

ControlDesigner类提供了AutoFormats属性,其提供了DesignerAutoFormat类的DesignerAutoFormatCollection集合.我们来看下相关的类.

DesignerAutoFormatCollection集合 

ASP.NET控件设计时支持之自动格式设置中DesignerAutoFormat 是一个基类,如果你想为你的控件在设计时提供格式化的功能,你可以从此类派生,你必须实现Apply方法,此方法会将相关联的控件设置样式.由于实现比较简单就不再多多了,就直接拿MSDN的例子来看吧. 注意给 IndentLabelDesigner 加上SupportsPreviewControl元数据,这样可以支持预览功能.

  1. [Designer(typeof(IndentLabelDesigner)),  
  2.       ToolboxData("﹤{0}:IndentLabel Runat=\"server\"﹥﹤/{0}:IndentLabel﹥")]  
  3.   public class IndentLabel : Label  
  4.   {  
  5.       [SupportsPreviewControl(true)]  
  6.       public class IndentLabelDesigner : LabelDesigner  
  7.       {  
  8.           private DesignerAutoFormatCollection _autoFormats = null;  
  9.  
  10.           public override DesignerAutoFormatCollection AutoFormats  
  11.           {  
  12.               get 
  13.               {  
  14.                   if (_autoFormats == null)  
  15.                   {  
  16.                       _autoFormats = new DesignerAutoFormatCollection();  
  17.                       _autoFormats.Add(new IndentLabelAutoFormat("MyClassic"));  
  18.                       _autoFormats.Add(new IndentLabelAutoFormat("MyBright"));  
  19.                       _autoFormats.Add(new IndentLabelAutoFormat("Default"));  
  20.                   }  
  21.                   return _autoFormats;  
  22.               }  
  23.           }  
  24.       }  
  25.  
  26.       private class IndentLabelAutoFormat : DesignerAutoFormat  
  27.       {  
  28.           public IndentLabelAutoFormat(string name)  
  29.               : base(name)  
  30.           { }  
  31.  
  32.           public override void Apply(Control inLabel)  
  33.           {  
  34.               if (inLabel is IndentLabel)  
  35.               {  
  36.                   IndentLabel ctl = (IndentLabel)inLabel;  
  37.  
  38.                   
  39.                   if (this.Name == "MyClassic")  
  40.                   {  
  41.                        
  42.                       ctl.ForeColor = Color.Gray;  
  43.                       ctl.BackColor = Color.LightGray;  
  44.                       ctl.Font.Size = FontUnit.XSmall;  
  45.                       ctl.Font.Name = "Verdana,Geneva,Sans-Serif";  
  46.                   }  
  47.                   else if (this.Name == "MyBright")  
  48.                   {  
  49.                        
  50.                       this.Style.ForeColor = Color.Maroon;  
  51.                       this.Style.BackColor = Color.Yellow;  
  52.                       this.Style.Font.Size = FontUnit.Medium;  
  53.                       ctl.MergeStyle(this.Style);  
  54.                   }  
  55.                   else 
  56.                   {  
  57.                       ctl.ForeColor = Color.Black;  
  58.                       ctl.BackColor = Color.Empty;  
  59.                       ctl.Font.Size = FontUnit.XSmall;  
  60.                   }  
  61.               }  
  62.           }  
  63.       }  
  64.   } 

这么着效果就实现了,这次比较懒,没好好写,还想打算写别的,就先这样吧.

ASP.NET控件设计时支持之自动格式设置的相关内容就向你介绍到这里,希望对你了解ASP.NET控件设计时支持之自动格式设置有帮助。

【编辑推荐】

  1. ASP.NET控件开发基础的总结详解
  2. ASP.NET模板控件开发浅析
  3. ASP.NET数据绑定控件开发浅析
  4. ASP.NET控件设计时支持浅析
  5. ASP.NET2.0数据源控件的用法浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-07 16:32:52

ASP.NET控件设计时支

2009-08-07 17:49:44

控件设计器

2009-08-07 17:17:43

ASP.NET控件设计

2009-08-07 17:59:35

控件设计器

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-28 16:21:03

Asp.net AjaAutoComplet

2009-07-27 17:25:53

ASP.NET验证控件

2009-08-06 15:21:45

ASP.NET控件开发RenderConte

2009-08-07 17:41:07

ASP.NET Web

2009-08-07 14:05:21

ASP.NET控件

2009-08-07 15:24:16

ASP.NET模板控件

2009-08-05 18:46:21

ComboBox显示ASP.NET控件开发

2009-08-06 09:18:01

ASP.NET自定义控ASP.NET控件开发

2009-08-05 16:53:14

ASP.NET组件设计

2009-08-10 14:08:15

ASP.NET服务器控ASP.NET组件设计

2009-08-04 17:28:45

Apache支持ASP

2009-08-10 14:38:29

ASP.NET组件设计

2009-08-04 15:20:59

ASP.NET数据验证数据验证控件

2009-08-07 15:34:15

ASP.NET数据绑定

2009-07-24 09:57:25

ASP.NET HTM
点赞
收藏

51CTO技术栈公众号