asp.net控件设计时支持(2)-自动格式设置

本文介绍如何使用ControlDesigner类的AutoFormats属性实现自动格式设置功能。通过创建自定义的IndentLabel控件及其设计师IndentLabelDesigner,文章展示了如何利用DesignerAutoFormat类来为控件提供多种预设样式。

       继续接着上面的

自动格式设置

先看个图



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

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




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

ContractedBlock.gifExpandedBlockStart.gif
None.gif  [Designer(typeof(IndentLabelDesigner)),
None.gif        ToolboxData(
"<{0}:IndentLabel Runat=\"server\"></{0}:IndentLabel>")]
None.gif    
public class IndentLabel : Label
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        [SupportsPreviewControl(
true)]
InBlock.gif        
public class IndentLabelDesigner : LabelDesigner
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
private DesignerAutoFormatCollection _autoFormats = null;
InBlock.gif
InBlock.gif            
public override DesignerAutoFormatCollection AutoFormats
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (_autoFormats == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        _autoFormats 
= new DesignerAutoFormatCollection();
InBlock.gif                        _autoFormats.Add(
new IndentLabelAutoFormat("MyClassic"));
InBlock.gif                        _autoFormats.Add(
new IndentLabelAutoFormat("MyBright"));
InBlock.gif                        _autoFormats.Add(
new IndentLabelAutoFormat("Default"));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return _autoFormats;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private class IndentLabelAutoFormat : DesignerAutoFormat
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public IndentLabelAutoFormat(string name)
InBlock.gif                : 
base(name)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{ }
InBlock.gif
InBlock.gif            
public override void Apply(Control inLabel)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (inLabel is IndentLabel)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IndentLabel ctl 
= (IndentLabel)inLabel;
InBlock.gif
InBlock.gif                  
InBlock.gif                    
if (this.Name == "MyClassic")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                       
InBlock.gif                        ctl.ForeColor 
= Color.Gray;
InBlock.gif                        ctl.BackColor 
= Color.LightGray;
InBlock.gif                        ctl.Font.Size 
= FontUnit.XSmall;
InBlock.gif                        ctl.Font.Name 
= "Verdana,Geneva,Sans-Serif";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (this.Name == "MyBright")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                       
InBlock.gif                        
this.Style.ForeColor = Color.Maroon;
InBlock.gif                        
this.Style.BackColor = Color.Yellow;
InBlock.gif                        
this.Style.Font.Size = FontUnit.Medium;
InBlock.gif                        ctl.MergeStyle(
this.Style);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ctl.ForeColor 
= Color.Black;
InBlock.gif                        ctl.BackColor 
= Color.Empty;
InBlock.gif                        ctl.Font.Size 
= FontUnit.XSmall;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

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

转载于:https://www.cnblogs.com/Clingingboy/archive/2007/04/01/695591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值