共用方式為


屬性和設計階段支援

設計階段支援擴充部分通常是在元件程式碼以外的程式碼中實作。 它會使用各種屬性,讓設計階段支援提供者與型別或型別的個別成員產生關聯。

與設計階段支援產生關聯的屬性

DesignerAttribute 能讓型別與設計工具產生關聯。 TypeConverterAttribute 能讓型別轉換子與型別或型別成員產生關聯。 EditorAttribute 能讓 UI 型別編輯器與型別或型別成員產生關聯。

自訂元件初始設定的屬性

您可以將 DefaultValueAttribute 套用至屬性,以指定在設計階段載入元件時設定的屬性預設值。 DefaultValueAttribute 會在設計階段覆寫元件初始化程式碼所設定的值,但此屬性不會覆寫設計工具所設定的值。

自訂屬性 (Property) 視窗行為的屬性 (Attribute)

您可以將 BrowsableAttribute 套用至 [屬性] 視窗,以指示是否應該在視窗中列出屬性或事件。 您也可以使用實作 IDesignerFilter 介面的設計工具,在設計階段修改公開至 [屬性] 視窗的屬性和事件集。 您可以將 CategoryAttribute 套用至屬性或事件,以指定屬性或事件應該列入 [屬性] 視窗中的分類。 您可以將 DescriptionAttribute 套用至屬性或事件,以指定 [屬性] 視窗中屬性或事件所要顯示的說明。

您可以將 DesignOnlyAttribute 套用至屬性,以指定屬性是否只能在設計階段設定。 您可以將 ReadOnlyAttribute 套用至屬性,以指定屬性在設計階段是唯讀或讀取/寫入。

您可以將 ParenthesizePropertyNameAttribute 套用至值為 true 的屬性,以指定是否應該在 [屬性] 視窗中以括弧括住名稱列出屬性。

您可以將 NotifyParentPropertyAttribute 套用至會引發告知的巢狀屬性,以指定是否要在巢狀屬性值變更時告知有巢狀或子屬性的屬性。

您可以將具有適當 RefreshProperties 值的 RefreshPropertiesAttribute 套用至屬性或事件,以指定是否應該重新整理元件的屬性、不重新整理任何屬性,或重新繪製設計工具檢視。

自訂設計階段序列化行為的屬性

您可以將具有適當 DesignerSerializationVisibility 列舉值的 DesignerSerializationVisibilityAttribute 套用至屬性,指定屬性值是否已序列化,或集合屬性值是否已序列化。 Visual Studio 中對此工作有相當廣泛的支援。 逐步解說:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合
逐步解說:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合
逐步解說:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合
逐步解說:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合

您可以透過將 SerializableAttribute 套用至型別,指定型別可序列化。 您可以透過實作 ISerializable 介面或提供自訂序列化程式,以提供自訂序列化。 如需序列化的詳細資訊,請參閱.NET Framework 中的序列化

如需有關常用設計階段屬性的詳細資訊,請參閱元件的設計階段屬性

套用屬性

設計階段屬性可套用於屬性、事件、類別,甚至組件。 下列範例示範將屬性 (Attribute) 套用於類別,然後套用於屬性 (Property) 和事件。

' The attribute is the element in angle brackets, and the parameters 
' in the attribute syntax are arguments of the constructor 
' of the attribute class.
' 
' Attributes applied at the class level.
<DefaultEvent("ValueChanged"), _
DefaultProperty("Number")> _
Public Class MyControl
   Inherits Control   
   ...
   ' Attribute applied to a property.
   <DefaultValue(False)> _
   Public Shadows ReadOnly Property TabStop() As Boolean
      ...
   End Property
   
   ' Attribute applied to a property.
   <CategoryAttribute("Data")> _
   Public ReadOnly Property Number() As Integer
      ...
   End Property 
   
   ' Attribute applied to an event.
   <Description("Raised when the Value displayed changes.")>  _
   Public Event ValueChanged As EventHandler
   ...
End Class
// The attribute is the element in brackets, and the parameters in 
// the attribute syntax are arguments of the constructor 
// of the attribute class.
// 
// Attributes applied at the class level.
[DefaultEvent("ValueChanged")]
[DefaultProperty("Number")]
public class MyControl : Control {
   ...
   // Attribute applied to a property.
   [DefaultValue(false)]
   public new bool TabStop {...
   }

   // Attribute applied to a property.
   [CategoryAttribute("Data")]
   public int Number {...}

   // Attribute applied to an event.
   [Description("Raised when the Value displayed changes.")]
   public event EventHandler ValueChanged;
}

依照慣例,屬性類別命名為 AttributeNameAttribute。 System.ComponentModel 命名空間包含許多基底屬性類別。

設計階段屬性和繼承

當您從具有設計階段屬性的基底元件衍生元件或控制項時,您的元件會繼承基底類別的設計階段功能。 如果基底功能足以應付您的用途,您不一定要重複套用屬性。 然而,您還是可以覆寫相同型別屬性或套用額外屬性於衍生元件。 下列程式碼片段將示範,以覆寫基底類別中套用的 BrowsableAttribute 屬性 (attribute),自訂覆寫繼承自 ControlText 屬性 (property) 的控制項。

Public Class MyControl
   Inherits Control
   ' The base class has [Browsable(true)] applied to the Text property.
   <Browsable(False)>  _
   Public Overrides Property [Text]() As String
      ...
   End Property 
   ...
End Class
public class MyControl : Control {
// The base class has [Browsable(true)] applied to the Text property.
[Browsable(false)]
 public override string Text {...}
...
}

套用型別轉換子、UI 型別編輯器或設計工具屬性

若要讓設計階段支援提供者與型別或型別成員產生關聯,請在類別宣告或成員宣告的上一行,套用適當的屬性型別。 下列程式碼範例示範套用至型別的 TypeConverterAttribute

<TypeConverter(GetType(MyColorConverter)), _
Editor(GetType(MyColorEditor), GetType(UITypeEditor))> _
Structure MyColor
   ...
End Structure
[ TypeConverter(typeof(MyColorConverter))]
[ Editor(typeof(MyColorEditor), typeof(UITypeEditor))] 
struct MyColor {...}

如果屬性 (Property) 的型別沒有相關的型別轉換子或 UI 型別編輯器,或如果您想要覆寫與屬性 (Property) 型別相關的預設型別轉換子或 UI 型別編輯器,您可以將屬性 (Attribute) 套用於屬性 (Property) 本身。 如果要將型別轉換子與屬性產生關聯,請將 TypeConverterAttribute 套用至屬性宣告,如下列程式碼範例所示。

<TypeConverter(GetType(PointConverter))> _
Public Property MyLocation() As Point
   ...
End Property       
[ TypeConverter(typeof(PointConverter))]
        public Point MyLocation {...}  

如果要將 UI 型別編輯器與屬性產生關聯,請將 EditorAttribute 套用至屬性,如下列程式碼範例所示。

<Editor(GetType(FlashTrackBarDarkenByEditor), _
GetType(UITypeEditor))>  _
Public Property DarkenBy() As Byte
   ...
End Property
[ Editor(typeof(FlashTrackBarDarkenByEditor), typeof(UITypeEditor))]
        public byte DarkenBy {...}

設計工具可以與型別 (而非屬性) 產生關聯。 若要將設計工具與型別產生關聯,請直接在類別宣告上方套用 DesignerAttribute,如下列程式碼範例所示。

<Designer(GetType(HelpLabel.HelpLabelDesigner))> _
Public Class HelpLabel
   Inherits System.Windows.Forms.Control
   Implements System.ComponentModel.IExtenderProvider
   ...
End Class
    [Designer(typeof(HelpLabel.HelpLabelDesigner))]
    public class HelpLabel : System.Windows.Forms.Control, System.ComponentModel.IExtenderProvider {...}
注意事項注意事項

在上述範例中,TypeConverterAttributeEditorAttributeDesignerAttribute 類別的建構函式會接受 Type 物件,做為其引數。如果型別位在與設計階段類別相同的組件中,這些屬性的建構函式表單就能運作。如果設計階段類別在不同組件,那麼就需要不同的屬性建構函式格式 (稱為組件限定格式),如下列範例所示。

<Designer("System.Windows.Forms.Design.DocumentDesigner, System.Design")>  _
Public Class MyForm
   Inherits Form
   ...
End Class
[Designer("System.Windows.Forms.Design.DocumentDesigner, System.Design")]
public class MyForm : Form {...}

組件層級的設計階段屬性

ASP.NET 提供組件層級屬性 (TagPrefixAttribute),此屬性可以讓控制項開發人員指定 ASP.NET 控制項的標記前置詞。 標記前置詞會由 Visual Studio 自動插入至控制項的 Register 指示詞,如此控制項就可以在具有預先指定之標記前置詞的頁面上透過宣告的方式使用 (<tagprefix:controlname runat = server />)。

注意事項注意事項

TagPrefixAttribute 只能在視覺化設計工具 (Visual Designer) 中運作。如果您使用文字編輯器 (例如 Notepad) 撰寫 ASP.NET 網頁,您需要親自在控制項的 Register 指示詞中指定標記前置詞和命名空間。

下列程式碼範例會示範如何套用 TagPrefixAttribute。 屬性建構函式的第一個引數指定命名空間,而第二個指定標記前置詞。

<assembly: TagPrefix("SimpleControls", "simple")>
Namespace SimpleControls
   <Designer("SimpleControl.Design.SimpleDesigner, SimpleControl")>  _
   Public Class SimpleControl
      Inherits System.Web.UI.WebControls.WebControl
      ...
   End Class 
End Namespace
[ assembly:TagPrefix("SimpleControls", "simple") ]
namespace SimpleControls {
    [
        Designer("SimpleControl.Design.SimpleDesigner, SimpleControl")
    ]
    public class SimpleControl : System.Web.UI.WebControls.WebControl {}
}

請參閱

工作

如何:實作類型轉換子

如何:實作 UI 類型編輯器

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

概念

Windows Form 控制項中的屬性

元件的設計階段屬性

其他資源

擴充設計階段支援

自訂設計工具