Walkthrough: Providing Custom Design-time Metadata
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
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 Visual Studio Settings.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2012 RC.
Creating the Custom Control
The first step is to create the project for the WPF custom control.
To create the custom control
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.
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.
Open the project properties and select the Build (Compile for Visual Basic) tab.
Set the project's output path to "bin\".
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
Add a new Class Library project in Visual Basic or Visual C# named TailspinToysControlLibrary.Design to the solution.
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.
Add references to the following WPF and XAML assemblies.
PresentationCore
PresentationFramework
System.Xaml
WindowsBase
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.
Add a reference to the TailspinToysControlLibrary project.
In Solution Explorer, change the name of the Class1 code file to RegisterMetadata.cs or RegisterMetadata.vb.
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(); } } } }
Build the solution.
Next Steps
This walkthrough gives you a solution that contains the basic implementation for a custom designer experience that works in Visual Studio and Expression Blend. You can expand on this solution by performing the following walkthrough.
You can register your control and design-time assemblies by using the AssemblyFoldersEx registration procedure. For more information, see Deploying a Custom Control and Design-time Assemblies.
You can download sample custom design-time implementations from the WPF Designer Extensibility Samples site.
See Also
Tasks
Walkthrough: Providing Metadata for Toolbox Icons
Walkthrough: Creating a Custom Toolbox Icon for a Control
Reference
Concepts
Deploying a Custom Control and Design-time Assemblies
Providing Design-time Metadata