Share via


AttributeCallbackBuilder Class

An instance of this class is passed to callback delegates to lazily populate the attributes for a type.

Namespace:  Microsoft.Windows.Design.Metadata
Assembly:  Microsoft.Windows.Design (in Microsoft.Windows.Design.dll)

Syntax

'Declaration
Public NotInheritable Class AttributeCallbackBuilder
'Usage
Dim instance As AttributeCallbackBuilder
public sealed class AttributeCallbackBuilder
public ref class AttributeCallbackBuilder sealed
public final class AttributeCallbackBuilder

Remarks

Use the AttributeCallbackBuilder to populate an AttributeTable on demand. This is especially important for attribute tables that hold many attributes. If you are specifying only a few attributes for a type, use the AttributeTableBuilder by itself.

Using the delegate format, no attributes are constructed until metadata for the target type is requested by the designer. The AttributeTableBuilder transfers this callback information into the AttributeTable and preserves it. Therefore, these callback delegates are only invoked on demand. Once invoked, their results are cached by the AttributeTable.

Enable on-demand attribute creation by using the AddCallback method.

Examples

The following code example shows how to create and populate an attribute table by using the AttributeCallbackBuilder class.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;

namespace CustomControlLibrary.VisualStudio.Design
{
    // Container for any general design-time metadata to initialize. 
    // Designers look for a type in the design-time assembly that  
    // implements IRegisterMetadata. If found, designers instantiate  
    // this class and call its Register() method automatically. 
    internal class Metadata : IRegisterMetadata
    {
        // Called by the designer to register any design-time metadata. 
        public void Register()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Build the attribute table by using the AttributeCallbackBuilder  
            // class. The attribute table is not populated until the designer 
            // needs it, which is more efficient for large attribute tables.
            builder.AddCallback(
                typeof(Button), 
                delegate(AttributeCallbackBuilder callbackBuilder)
            {
                callbackBuilder.AddCustomAttributes(
                    new DefaultPropertyAttribute("Content"));

                // Apply the ReadOnlyAttribute to the Background property  
                // of the Button class.
                callbackBuilder.AddCustomAttributes(
                    "Background",
                    new ReadOnlyAttribute(true));

                PropertyDescriptorCollection properties =
                    TypeDescriptor.GetProperties(typeof(Button));
                PropertyDescriptor pd = properties["Foreground"];
                callbackBuilder.AddCustomAttributes(
                    pd,
                    new ReadOnlyAttribute(true));

                callbackBuilder.AddCustomAttributes(
                   Button.WidthProperty,
                   new TypeConverterAttribute(typeof(LengthConverter)),
                   new DescriptionAttribute("This is the width"));

                MemberInfo[] members = typeof(Button).GetMember("Height");
                callbackBuilder.AddCustomAttributes(
                    members[0],
                    new ReadOnlyAttribute(true));
            });

            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
    }
}

Inheritance Hierarchy

System.Object
  Microsoft.Windows.Design.Metadata.AttributeCallbackBuilder

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

AttributeCallbackBuilder Members

Microsoft.Windows.Design.Metadata Namespace

AttributeTableBuilder

AddCallback

AttributeTable

Other Resources

Metadata Store

Walkthrough: Creating a Design-time Adorner