ASP.NET控件設(shè)計(jì)時(shí)支持之自動(dòng)格式設(shè)置淺析
ASP.NET控件設(shè)計(jì)時(shí)支持之自動(dòng)格式設(shè)置是如何實(shí)現(xiàn)的呢?
先看個(gè)圖
相信大家都很熟悉吧,我們可以用這個(gè)面板很方面的使用預(yù)定的樣式.我們可以稱之為自動(dòng)格式設(shè)置或者自動(dòng)套用樣式.
ControlDesigner類提供了AutoFormats屬性,其提供了DesignerAutoFormat類的DesignerAutoFormatCollection集合.我們來看下相關(guān)的類.
ASP.NET控件設(shè)計(jì)時(shí)支持之自動(dòng)格式設(shè)置中DesignerAutoFormat 是一個(gè)基類,如果你想為你的控件在設(shè)計(jì)時(shí)提供格式化的功能,你可以從此類派生,你必須實(shí)現(xiàn)Apply方法,此方法會(huì)將相關(guān)聯(lián)的控件設(shè)置樣式.由于實(shí)現(xiàn)比較簡單就不再多多了,就直接拿MSDN的例子來看吧. 注意給 IndentLabelDesigner 加上SupportsPreviewControl元數(shù)據(jù),這樣可以支持預(yù)覽功能.
- [Designer(typeof(IndentLabelDesigner)),
- ToolboxData("﹤{0}:IndentLabel Runat=\"server\"﹥﹤/{0}:IndentLabel﹥")]
- public class IndentLabel : Label
- {
- [SupportsPreviewControl(true)]
- public class IndentLabelDesigner : LabelDesigner
- {
- private DesignerAutoFormatCollection _autoFormats = null;
- public override DesignerAutoFormatCollection AutoFormats
- {
- get
- {
- if (_autoFormats == null)
- {
- _autoFormats = new DesignerAutoFormatCollection();
- _autoFormats.Add(new IndentLabelAutoFormat("MyClassic"));
- _autoFormats.Add(new IndentLabelAutoFormat("MyBright"));
- _autoFormats.Add(new IndentLabelAutoFormat("Default"));
- }
- return _autoFormats;
- }
- }
- }
- private class IndentLabelAutoFormat : DesignerAutoFormat
- {
- public IndentLabelAutoFormat(string name)
- : base(name)
- { }
- public override void Apply(Control inLabel)
- {
- if (inLabel is IndentLabel)
- {
- IndentLabel ctl = (IndentLabel)inLabel;
- if (this.Name == "MyClassic")
- {
- ctl.ForeColor = Color.Gray;
- ctl.BackColor = Color.LightGray;
- ctl.Font.Size = FontUnit.XSmall;
- ctl.Font.Name = "Verdana,Geneva,Sans-Serif";
- }
- else if (this.Name == "MyBright")
- {
- this.Style.ForeColor = Color.Maroon;
- this.Style.BackColor = Color.Yellow;
- this.Style.Font.Size = FontUnit.Medium;
- ctl.MergeStyle(this.Style);
- }
- else
- {
- ctl.ForeColor = Color.Black;
- ctl.BackColor = Color.Empty;
- ctl.Font.Size = FontUnit.XSmall;
- }
- }
- }
- }
- }
這么著效果就實(shí)現(xiàn)了,這次比較懶,沒好好寫,還想打算寫別的,就先這樣吧.
ASP.NET控件設(shè)計(jì)時(shí)支持之自動(dòng)格式設(shè)置的相關(guān)內(nèi)容就向你介紹到這里,希望對你了解ASP.NET控件設(shè)計(jì)時(shí)支持之自動(dòng)格式設(shè)置有幫助。
【編輯推薦】