Udostępnij za pośrednictwem


How to: Use the Metadata Store

When you use extensibility to customize the Windows Presentation Foundation (WPF) Designer for Visual Studio, you will often create custom controls. The code for your controls and the metadata defining the design-time behavior of your controls is factored into separate assemblies. The metadata is factored into the MetadataStore. For more information, see WPF Designer Extensibility Architecture.

The metadata store contains information about design-time behavior such as custom adorners, custom context menus, and custom property editors. The metadata store is implemented as code-based attribute tables.

Note

Metadata assemblies are loaded in the following order: *.Design.dll first, *.VisualStudio.Design.dll or *.Expression.Design.dll second. This allows designer-specific metadata to override the common shared metadata.

Adding Custom Attribute Tables to the Metadata Store

When a designer loads a custom control, it looks for a type in the corresponding design-time assembly that implements IRegisterMetadata. If found, it instantiates the type and calls its Register method automatically.

To add custom attribute tables to the metadata store

  1. In the general design-time assembly for your control (<Your Control>.Design.dll), add a file that is named Metadata.cs or Metadata.vb.

  2. In the Metadata file, add a class that implements IRegisterMetadata, and implement the Register method.

  3. Instantiate an AttributeTableBuilder object and call the AddCustomAttributes method to add the attributes to it.

  4. Call the AddAttributeTable method to add the AttributeTable to the metadata store.

  5. Repeat steps 1 through 4 for the Visual Studio-specific design-time assembly (<Your Control>.VisualStudio.Design.dll).

Example

The following example adds metadata for a custom control. The code connects a custom property editor to a property of the custom control.

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

The following example adds metadata for a custom control. The code connects custom adorners and a custom context menu to the custom control.

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

See Also

Concepts

Metadata Store

Reference

AdornerProvider

ContextMenuProvider

Other Resources

Basic Extensibility Concepts

Understanding WPF Designer Extensibility

WPF Designer Extensibility