元件的設計階段屬性

如果您不熟悉套用屬性以提供中繼資料給 Common Language Runtime,請參閱使用屬性擴充中繼資料。 由於元件可以顯示在設計工具中 (例如 Visual Studio),所以它們需要能提供中繼資料給設計階段工具的屬性。 這一節將描述並提供常用的設計階段屬性清單。

屬性和設計工具支援

設計階段屬性是在設計階段正確顯示控制項及其成員所不可或缺者,因為它們可提供重要的資訊給視覺化設計工具。

在下列程式碼片段中,CategoryAttribute 屬性會讓屬性瀏覽器顯示 Alignment 分類中的 TextAlignment 屬性。 DescriptionAttribute 屬性 (Attribute) 會讓屬性瀏覽器在使用者按下屬性 (Property) 時,提供這個屬性的簡短描述。

[
Category("Alignment"),
Description("Specifies the alignment of text.")
]
public ContentAlignment TextAlignment { //... }
<Category("Alignment"), _
Description("Specifies the alignment of text.")> _
Public Property _
TextAlignment As ContentAlignment
   ' ...
End Property
注意事項注意事項

在 Visual C# 和 Visual Basic 中,可將命名為 AttributeNameAttribute 的屬性類別單純地當做屬性語法中的 AttributeName 來參考。

有些設計階段屬性是在類別層級套用。 DesignerAttribute 屬性是在類別層級套用,並且會告訴表單設計工具使用哪一個設計工具類別來顯示控制項。 元件是與預設設計工具 (System.ComponentModel.Design.ComponentDesigner) 關聯,而 Windows Form 和 ASP.NET 伺服器控制項是與它們自己的預設設計工具關聯。 只有在您定義元件或控制項的自訂設計工具時,才需套用 DesignerAttribute

// Associates the designer class SimpleControl.Design.SimpleDesigner
// with Simple.
[ Designer(typeof(SimpleControl.Design.SimpleDesigner))]
    public class Simple : WebControl { //... }
' Associates the designer class SimpleControl.Design.SimpleDesigner
' with Simple.
<Designer(GetType(SimpleControl.Design.SimpleDesigner))> _
Public Class Simple
    Inherits WebControl
    ' ...
End Class

屬性和事件的通用屬性

下表所列是通常套用至屬性 (Property) 和事件的屬性 (Attribute)。

屬性

套用至

描述

BrowsableAttribute

屬性和事件

指定屬性或事件是否應該在屬性瀏覽器中顯示。

CategoryAttribute

屬性和事件

指定要在其中群組屬性或事件的分類名稱。 使用分類時,元件屬性和事件可以在屬性瀏覽器中顯示為邏輯群組。

DescriptionAttribute

屬性和事件

定義要在使用者選取屬性或事件時,顯示在屬性瀏覽器底部的小文字方塊。

BindableAttribute

屬性

指定屬性是否應被繫結。

DefaultPropertyAttribute

屬性

(在類別宣告之前插入這個屬性)

指定元件的預設屬性。 當使用者按下控制項時,這個屬性會在屬性瀏覽器中被選取。

DefaultValueAttribute

屬性

設定屬性的簡單預設值。

EditorAttribute

屬性

指定在視覺化設計工具中用來編輯 (變更) 屬性的編輯器。

LocalizableAttribute

屬性

指定屬性可以當地語系化。 具有這個屬性 (Attribute) 的任何屬性 (Property) 會在使用者選擇要當地語系化表單時,自動保存 (Persist) 至資源檔中。

DesignerSerializationVisibilityAttribute

屬性

指定顯示在屬性瀏覽器中的屬性是否應該 (及如何) 保存至程式碼中。

TypeConverterAttribute

屬性

指定用來將屬性型別轉換成其他資料型別的型別轉換子。

DefaultEventAttribute

事件

(在類別宣告之前插入這個屬性)

指定元件的預設事件。 這是當使用者按下元件時,在屬性瀏覽器中被選取的事件。

除非另有說明,否則屬性 (Property) 和事件的屬性 (Attribute) 在程式碼中是直接置於屬性 (Property) 和事件宣告的前面,如下列範例中所示。

// To apply CategoryAttribute to the BorderColor 
// property, place it immediately before the declaration
// of the BorderColor property.
[Category("Appearance")] 
public Color BorderColor;

// To apply DescriptionAttribute to the Click event, 
// place it immediately before the declaration
// of the Click event.
[Description("The Click event of the button")]
public event EventHandler Click;
' To apply CategoryAttribute  to the BorderColor 
' property, place it before the property declaration.
<Category("Appearance")> Public BorderColor As Color

' To apply DescriptionAttribute to the Click event, 
' place it before the event declaration.
<Description("The Click event of the button")> Public Event Click

如需讓設計工具與元件和控制項產生關聯之設計階段屬性的詳細資訊,請參閱擴充設計階段支援

除了使用 .NET Framework 類別庫中定義的屬性類別以外,您也可以定義自己的屬性類別。 如需詳細資訊,請參閱您程式語言的文件或參閱撰寫自訂屬性

請參閱

工作

如何:在 Windows Form 控制項中套用屬性

概念

屬性和設計階段支援

Windows Form 控制項中的屬性

其他資源

擴充設計階段支援