Walkthrough: Providing Custom Design-time Metadata

The WPF Designer for Visual Studio enables you to provide custom design-time metadata for different designers. For example, you can target different design experiences for your custom controls in Visual Studio and Expression Blend. To register your custom design-time metadata with the designer, you implement the IProvideAttributeTable interface and call one of the AddCustomAttributes methods.

This walkthrough shows how to provide custom design-time implementations for a WPF or Silverlight custom control library. In this walkthrough, you perform the following tasks:

  • Create a WPF custom control library project.

  • Create a separate assembly for design-time metadata.

When you are finished, you will know how to provide custom design-time metadata for a WPF or Silverlight custom control.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Creating the Custom Control

The first step is to create the project for the WPF custom control.

To create the custom control

  1. Create a new WPF Custom Control Library project in Visual Basic or Visual C# named TailspinToysControlLibrary.

    The code for CustomControl1 opens in the Code Editor.

    Note

    If your custom design experience only needs to target Expression Blend 4 and Visual Studio 2010, set the project's target framework to .NET Framework 4. If your custom design experience must target Expression Blend 3, Expression Blend 4, and Visual Studio 2010, set the project's target framework to .NET Framework 3.5.

  2. In Solution Explorer, change the name of the code file to TailspinToysControl.cs or TailspinToysControl.vb. If a message box appears that asks if you want to perform a rename for all references in this project, click Yes.

  3. Open the project properties and select the Build (Compile for Visual Basic) tab.

  4. Set the project's output path to "bin\".

  5. Build the solution.

Creating the Design-time Metadata Assembly

Design-time code is deployed in special metadata assemblies. For this walkthrough, the custom metadata is supported by Visual Studio and Expression Blend and is deployed in an assembly named TailspinToysControlLibrary.Design. For more information on naming assemblies, see Deploying a Custom Control and Design-time Assemblies.

To create the design-time metadata assembly

  1. Add a new Class Library project in Visual Basic or Visual C# named TailspinToysControlLibrary.Design to the solution.

  2. Set the project's output path to "..\TailspinToysControlLibrary\bin\". This keeps the control's assembly and the metadata assembly in the same folder, which enables metadata discovery for designers.

  3. Add references to the following WPF and XAML assemblies.

    • PresentationCore

    • PresentationFramework

    • System.Xaml

    • WindowsBase

  4. Add references to the following WPF Designer assemblies. 

    • Microsoft.Windows.Design.Extensibility

    • Microsoft.Windows.Design.Interaction

    Note

    If you have Expression Blend installed, you may see two sets of WPF Designer assemblies in the Add References dialog box. Select the two assemblies that are installed with Visual Studio, located in the $(VSInstallDir)\Common7\IDE\PublicAssemblies folder.

    Note

    If the WPF Designer assemblies do not appear in the Add References dialog box. Click the Browse tab and navigate to the assemblies in the $(VSInstallDir)\Common7\IDE\PublicAssemblies folder. 

  5. Add a reference to the TailspinToysControlLibrary project.

  6. In Solution Explorer, change the name of the Class1 code file to RegisterMetadata.cs or RegisterMetadata.vb.

  7. Replace the automatically generated code with the following code. This code creates an AttributeTable which attaches the custom design-time attributes to the TailspinToysControl class.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Microsoft.Windows.Design;
    using Microsoft.Windows.Design.Features;
    using Microsoft.Windows.Design.Metadata;
    
    using TailspinToysControlLibrary;
    
    // The ProvideMetadata assembly-level attribute indicates to designers
    // that this assembly contains a class that provides an attribute table. 
    [assembly: ProvideMetadata(typeof(TailspinToysControlLibrary.Design.RegisterMetadata))]
    namespace TailspinToysControlLibrary.Design
    {
        internal class RegisterMetadata : IProvideAttributeTable
        {
            // Called by the designer to register any design-time metadata. 
            public AttributeTable AttributeTable
            {
                get
                {
                    AttributeTableBuilder builder = new AttributeTableBuilder();
    
                    // Set ToolboxBrowsableAttribute to true to display your custom control 
                    // in the Toolbox and in the Choose Items... dialog box. 
                    builder.AddCustomAttributes(typeof(TailspinToysControl), new ToolboxBrowsableAttribute(true));
    
                    return builder.CreateTable();
                }
            }
        }
    }
    
  8. Build the solution.

Next Steps

See Also

Tasks

Walkthrough: Providing Metadata for Toolbox Icons

Walkthrough: Creating a Custom Toolbox Icon for a Control

Reference

AttributeTable

WPF Components Tab, Choose Toolbox Items Dialog Box

Concepts

Toolbox Icons

Deploying a Custom Control and Design-time Assemblies

Providing Design-time Metadata