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

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

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

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 インターフェイスを実装するか、カスタム シリアライザーを提供します。 シリアル化の詳細については、「Serialization」を参照してください。

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

属性の適用

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

' 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 によってコントロールの 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 フォーム コントロールの属性

コンポーネントのデザイン時属性

その他の技術情報

デザイン時サポートの拡張

カスタム デザイナー