开发者

Recreating "Font" Property Behavior

Controls that inherit off of System.Web.UI.WebControls.WebControl have a property called Font. The type is System.Web.Ui.WebControls.FontInfo.

When work开发者_如何学Cing with these controls in the designer, it breaks out the Font property into multiple properties like Font-Bold, Font-Italic, etc. When working with these same WebControls in the codebehind, there's only a Font property (no Font-Bold, Font-Italic, etc).

How can this behavior be manually recreated when creating WebControls? Specifically, what combination of System.ComponentModel attributes can show/hide these properties in Intellisense?


You should be able to access Bold, Italic and so on as boolean properties:

http://msdn.microsoft.com/it-it/library/system.web.ui.webcontrols.fontinfo.aspx

  void Page_Load(object sender, EventArgs e)
  {
    // When the page loads, set the the myLabel Label control's FontInfo properties.
    // Note that myLabel.Font is a FontInfo object.

    myLabel.Font.Bold = true;
    myLabel.Font.Italic = false;
    myLabel.Font.Name = "verdana";
    myLabel.Font.Overline = false;
    myLabel.Font.Size = 10;
    myLabel.Font.Strikeout = false;
    myLabel.Font.Underline = true;

    // Write information on the FontInfo object to the myLabel label.
    myLabel.Text = myLabel.Font.ToString();

  }


The property-breakdown is happening automatically.

If you have a control that has a property that has properties of its own

public class ServerControl1 : WebControl
{
   public CompositeItem Composite { get; set; }

    public ServerControl1()
    {
        Composite = new CompositeItem();
    }
}

public class CompositeItem
{
    public bool ItemOne { get; set; }
    public string ItemTwo { get; set; }
    public int ItemThree { get; set; }
}

you can use the Font-Bold syntax in the aspx, meaning that

<cc:ServerControl1 runat="server" ID="scOne" 
    Composite-ItemOne="true" Composite-ItemTwo ="stringx"/>

will work as expected. However, autocomplete does not work, and I'm not sure which combination of System.ComponentModel attributes is required to make it behave like the Font-Bold.


The property that you'd like to expand (Font in this case) should have attribute System.ComponentModel.DesignerSerializationVisibility set to System.ComponentModel.DesignerSerializationVisibility.Content. This is detailed in the following link

System.ComponentModel.DesignerSerializationVisibility

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜