[英]ASP.NET Custom WebControl Nested Properties as Object
我正在嘗試實現具有“嵌套”屬性的自定義復合Web控件,即將一組屬性封裝到一個類中。
例如,在此復合控件中,我放置了一個按鈕。 我希望能夠將按鈕的相關屬性封裝到一個類中(例如,buttonText,buttonStyle等)。 這將使在多按鈕/控件復合控件中定義屬性更加容易,一致和直觀。
注意:我希望封裝的屬性以與樣式/字體非常相似的方式分組顯示在VisualStudio的“屬性”對話框中。
樣品:
public class fooButtonProperties
{
[Category("Appearance"), Description("URL for the Profile page")]
public string URL { get; set; }
[Category("Appearance"), Description("Text to display"), DefaultValue("Profile")]
public string ButtonText { get; set; }
/// <summary>
/// Position of the control on the page, default is Right-Aligned
/// </summary>
[Category("Appearance"), Description("Position in the Header"), DefaultValue(PIONEERFramework.Web.UI.WebControls.PageHeaderFooter.Classes.DesignEnum.DesignLayoutEnums.HorizontalPositions.Right)]
///Here is the composite control
public PIONEERFramework.Web.UI.WebControls.PageHeaderFooter.Classes.DesignEnum.DesignLayoutEnums.HorizontalPositions PositionInHeader { get; set; }
}
public class myCustomClass: System.Web.UI.WebControls.CompositeControl
{
protected System.Web.UI.HtmlControls.HtmlLink myButton;
[Category("Appearance")]
public fooButtonProperties myButtonProperties { get { return _profileButtonProp; } }
private fooButtonProperties _myeButtonProp;
#region Constructor
public myCustomClass()
{
this._myeButtonProp = new fooButtonProperties ();
}
#endregion
}
不幸的是,這種方法行不通。 新屬性myButtonProperties根本不會出現在“屬性”對話框中。
要創建嵌套屬性,請使用控件中的System.ComponentModel.DesignerSerializationVisibility
屬性,如下所示:
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public fooButtonProperties myButtonProperties { get { return _profileButtonProp; } }
最終的屬性名稱將為“ myButtonProperties-URL”(帶連字符)。 您也可以將此屬性添加到fooButtonProperties
類的屬性中,以進行更多的嵌套。
請注意,您可能必須關閉aspx文件並重建解決方案以刷新“屬性”窗口。
Category
屬性可在您的控件和嵌套類中使用。
Description
屬性似乎是正確的,但是它不起作用,這可能是Visual Studio中的錯誤。 我找到了此鏈接: https : //www.beta.microsoft.com/VisualStudio/feedback/details/653335/webcontrol-property-descriptions-do-not-appear-in-property-window
我也觀察到沒有屬性顯示描述。
問候
奧利
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.