How is automatic format settings supported by ASP. NET controls designed?
First look at a chart
I believe everyone is familiar with it. We can use this Panel to use the predefined style. We can call it automatic format setting or automatic style apply.
The ControlDesigner class provides the AutoFormats attribute, which provides the DesignerAutoFormatCollection set of the DesignerAutoFormat class. Let's take a look at the related classes.
ASP. in the automatic format settings supported by the. NET control during design, DesignerAutoFormat is a base class. If you want to provide the Formatting Function for your control during design, you can derive from this class, you must implement the Apply method, which sets the style of the associated control. since the implementation is relatively simple, there will be no more, let's look at the MSDN example directly. note that SupportsPreviewControl metadata is added to IndentLabelDesigner to support preview.
- [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;
- }
- }
- }
- }
- }
The effect is achieved. This time it is relatively lazy, not well written, and I want to write something else. Let's do it first.
This section describes the content related to automatic format settings supported by ASP. NET controls during design. It is helpful to understand the automatic format settings supported by ASP. NET controls during design.
- Summary of ASP. NET control development
- Development of ASP. NET template controls
- Development of ASP. NET data binding controls
- Analysis of ASP. NET control design support
- Usage of ASP. NET2.0 data source controls