属性とデザイン時サポート

デザイン時サポート拡張機能は、通常、コンポーネントのコードとは別に存在するコードに実装されます。デザイン時サポート プロバイダを型や型の個々のメンバに関連付けるために、さまざまな属性を使用します。

デザイン時サポートを関連付けるための属性

DesignerAttribute はデザイナを型に関連付けます。TypeConverterAttribute は型コンバータを型または型のメンバに関連付けます。EditorAttribute は UI 型エディタを型または型のメンバに関連付けます。

コンポーネントの初期化をカスタマイズするための属性

デザイン時にコンポーネントを読み込んだときに設定されるプロパティの既定値を指定するには、プロパティに DefaultValueAttribute を適用します。DefaultValueAttribute は、デザイン時にコンポーネントの初期化コードによって設定された値をオーバーライドしますが、デザイナによって設定された値はオーバーライドしません。

[プロパティ] ウィンドウの動作をカスタマイズするための属性

プロパティやイベントを [プロパティ] ウィンドウに表示するかどうかを指定するには、BrowsableAttribute を適用します。また、IDesignerFilter インターフェイスを実装するデザイナを使用して、デザイン時に [プロパティ] ウィンドウに公開されるプロパティやイベントのセットを変更することもできます。[プロパティ] ウィンドウでプロパティやイベントが表示されるカテゴリを指定するには、プロパティやイベントに CategoryAttribute を適用します。[プロパティ] ウィンドウでプロパティやイベントに関する説明を表示するように指定するには、プロパティやイベントに DescriptionAttribute を適用します。

プロパティをデザイン時にだけ設定できるようにするかどうかを指定するには、プロパティに DesignOnlyAttribute を適用します。デザイン時にプロパティが読み取り専用であるか、または読み書き可能であるかを指定するには、プロパティに ReadOnlyAttribute を適用します。

[プロパティ] ウィンドウでプロパティの名前をかっこで囲んで表示するように指定するには、プロパティに ParenthesizePropertyNameAttribute の値を true にして適用します。

入れ子になった子プロパティを持つプロパティに、入れ子になったプロパティの値が変更された場合に通知するように指定するには、通知を発生させる入れ子になったプロパティに NotifyParentPropertyAttribute を適用します。

コンポーネントのプロパティを更新するかどうか、またはデザイナ ビューを再描画するかどうかを指定するには、RefreshProperties を適切な値に設定した RefreshPropertiesAttribute をプロパティやイベントに適用します。

デザイン時のシリアル化の動作をカスタマイズするための属性

プロパティの値をシリアル化するかどうか、つまりコレクション プロパティの値をシリアル化するかどうかを指定するには、適切な DesignerSerializationVisibility 列挙値を設定した DesignerSerializationVisibilityAttribute をプロパティに適用します。Visual Studio では、このタスクに対する広範なサポートが用意されています。 チュートリアル : DesignerSerializationVisibilityAttribute を使用した、標準データ型のコレクションのシリアル化
チュートリアル : DesignerSerializationVisibilityAttribute を使用した、標準データ型のコレクションのシリアル化
チュートリアル : DesignerSerializationVisibilityAttribute を使用した、標準データ型のコレクションのシリアル化
チュートリアル : DesignerSerializationVisibilityAttribute を使用した、標準データ型のコレクションのシリアル化

型をシリアル化できるように指定するには、型に SerializableAttribute を適用します。カスタマイズしたシリアル化を提供するには、ISerializable インターフェイスを実装するか、カスタム シリアライザを提供します。シリアル化の詳細については、「シリアル化」を参照してください。

一般的に使用されるデザイン時属性の詳細については、「コンポーネントのデザイン時属性」を参照してください。

属性の適用

デザイン時属性は、プロパティ、イベント、クラス、アセンブリに適用されます。属性をクラスに適用し、続いてプロパティとイベントに適用するコード例を次に示します。

' 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 名前空間には、多数の基本属性クラスが含まれています。

デザイン時属性と継承

デザイン時属性を持つ基本コンポーネントから派生したコンポーネントまたはコントロールには、基本クラスのデザイン時機能が継承されます。基本クラスの機能がコンポーネントの用途に対して十分である場合は、属性を適用し直す必要はありません。ただし、派生コンポーネントでは、同じ型の属性をオーバーライドしたり、他の属性を適用したりできます。Control から継承した Text プロパティをオーバーライドするために、基本クラスに適用されている BrowsableAttribute 属性をオーバーライドするカスタム コントロールのコードを次に示します。

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 {...}

プロパティの型に型コンバータまたは UI 型エディタが関連付けられていない場合、またはプロパティの型に関連付けられている既定の型コンバータまたは UI 型エディタをオーバーライドする場合は、そのプロパティ自体に属性を適用できます。型コンバータをプロパティに関連付けるには、次のコード例に示すように 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 {...}

注意

上記の例では、TypeConverterAttributeEditorAttribute、および DesignerAttribute クラスのコンストラクタは、引数として System.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 が提供するアセンブリ レベルの属性 (System.Web.UI.TagPrefixAttribute) を使用すると、コントロール開発者は ASP.NET コントロールのタグ プリフィックスを指定できます。タグ プリフィックスは、Visual Studio .NET によってコントロールの Register ディレクティブに自動的に挿入されます。このため、事前に指定されたタグ プリフィックス (<tagprefix:controlname runat = server />) を使用して、コントロールをページ上で宣言によって使用できます。

注意

TagPrefixAttribute は、ビジュアル デザイナでのみ動作します。メモ帳などのテキスト エディタを使用して ASP.NET ページを編集する場合は、コントロールの Register ディレクティブ内にタグ プリフィックスと名前空間を指定する必要があります。

TagPrefixAttribute を適用する方法を次のコード例に示します。属性のコンストラクタへの 1 番目の引数で名前空間を指定し、2 番目の引数でタグ プリフィックスを指定しています。

<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 フォーム コントロールに属性を適用する

概念

Windows フォーム コントロールの属性
コンポーネントのデザイン時属性

その他の技術情報

デザイン時サポートの拡張
カスタム デザイナ