更新 : 2007 年 11 月
機能拡張を使用して Windows Presentation Foundation (WPF) Designer for Visual Studio をカスタマイズするときに、カスタム コントロールを作成することがよくあります。コントロールのコードと、コントロールのデザイン時の動作を定義するメタデータは、それぞれ別のアセンブリに組み込まれます。メタデータは、MetadataStore に組み込まれます。詳細については、「WPF デザイナの機能拡張アーキテクチャ」を参照してください。
メタデータ ストアには、カスタム装飾、カスタム コンテキスト メニュー、カスタム プロパティ エディタなど、デザイン時の動作に関する情報が格納されます。メタデータ ストアは、コード ベースの属性テーブルとして実装されます。
![]() |
---|
メタデータ アセンブリの読み込み順序は、*.Design.dll が最初で、*.VisualStudio.Design.dll または *.Expression.Design.dll が 2 番目です。これにより、デザイナ固有メタデータが共有メタデータよりも優先されます。 |
メタデータ ストアへのカスタム属性テーブルの追加
デザイナは、カスタム コントロールを読み込むときに、そのコントロールに対応する IRegisterMetadata 実装デザイン時アセンブリで型を探します。見つかれば、その型を自動的にインスタンス化し、Register メソッドを呼び出します。
メタデータ ストアにカスタム属性テーブルを追加するには
コントロールの共通のデザイン時アセンブリ (<Your Control>.Design.dll) で、Metadata.cs または Metadata.vb という名前のファイルを追加します。
この Metadata ファイルで、IRegisterMetadata を実装するクラスを追加し、Register メソッドを実装します。
AttributeTableBuilder オブジェクトをインスタンス化し、AddCustomAttributes メソッドを呼び出して属性をオブジェクトに追加します。
AddAttributeTable メソッドを呼び出して、AttributeTable をメタデータ ストアに追加します。
Visual Studio 固有デザイン時アセンブリ (<Your Control>.VisualStudio.Design.dll) に対して、手順 1. ~ 4. を繰り返します。
使用例
カスタム コントロールにメタデータを追加する方法を次の例に示します。このコードでは、カスタム プロパティ エディタをカスタム コントロールのプロパティに接続します。
internal class Metadata : Microsoft.Windows.Design.Metadata.IRegisterMetadata
{
public void Register()
{
Microsoft.Windows.Design.Metadata.AttributeTableBuilder builder = new Microsoft.Windows.Design.Metadata.AttributeTableBuilder();
//Property Editor
builder.AddCustomAttributes(typeof(<Your Custom Control>), <Property>, new System.ComponentModel.EditorAttribute(typeof(<Your Custom Editor>), typeof(<Your Custom Editor>)));
//Category Editor
builder.AddCustomAttributes(typeof(<Your Custom Control>), new System.ComponentModel.EditorAttribute(typeof(<Your Custom Editor>), typeof(<Your Custom Editor>)));
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable());
}
}
Friend Class Metadata
Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata
Public Sub Register() Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata.Register
Dim builder As New Microsoft.Windows.Design.Metadata.AttributeTableBuilder()
'Property Editor
builder.AddCustomAttributes(GetType(<Your Custom Control>), <Property>, New System.ComponentModel.EditorAttribute(GetType(<Your Custom Editor>), GetType(<Your Custom Editor>)))
'Category Editor
builder.AddCustomAttributes(GetType(<Your Custom Control>), New System.ComponentModel.EditorAttribute(GetType(<Your Custom Editor>), GetType(<Your Custom Editor>)))
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable())
End Sub
End Class
カスタム コントロールにメタデータを追加する方法を次の例に示します。このコードでは、カスタム修飾とカスタム コンテキスト メニューをカスタム コントロールに接続します。
internal class Metadata : Microsoft.Windows.Design.Metadata.IRegisterMetadata
{
public void Register()
{
Microsoft.Windows.Design.Metadata.AttributeTableBuilder builder = new Microsoft.Windows.Design.Metadata.AttributeTableBuilder();
//Adorners
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Adorner Provider>)));
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Adorner Provider>)));
//MenuActions
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Context Menu Provider>)));
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable());
}
}
Friend Class Metadata
Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata
Public Sub Register() Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata.Register
Dim builder As New Microsoft.Windows.Design.Metadata.AttributeTableBuilder()
'Adorners
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Adorner Provider>)))
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Adorner Provider>)))
'MenuActions
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Context Menu Provider>)))
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable())
End Sub
End Class