Share via


AttributeTableBuilder Class

Creates an attribute table that can be passed to the metadata store.

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

Syntax

'Declaration
Public Class AttributeTableBuilder
'Usage
Dim instance As AttributeTableBuilder
public class AttributeTableBuilder
public ref class AttributeTableBuilder
public class AttributeTableBuilder

Remarks

To create an attribute table, you start by creating an instance of the AttributeTableBuilder class. You add metadata to the attribute table builder by calling the AddCustomAttributes overloads. When you are finished adding metadata, you produce an attribute table from the attribute table builder by calling the CreateTable method. The attribute table builder methods support callback delegates, so the creation of the attribute table can be deferred until needed.

Use the AttributeCallbackBuilder class instead of the AttributeTableBuilder class if you are adding many attributes. This approach defers the creation of attributes until the designer requests metadata for the target type.

Examples

The following code example shows how to use the AttributeTableBuilder class to create and populate an attribute table. For more information, see Walkthrough: Creating a Design-time Adorner.

Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Reflection
Imports System.Text
Imports System.Windows.Media
Imports System.Windows.Controls
Imports System.Windows

Imports Microsoft.Windows.Design.Features
Imports Microsoft.Windows.Design.Metadata

' 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. 
Friend Class Metadata
    Implements IRegisterMetadata

    ' Called by the designer to register any design-time metadata. 
    Public Sub Register() Implements IRegisterMetadata.Register
        Dim builder As New AttributeTableBuilder()

        builder.AddCustomAttributes( _
            GetType(Button), _
            New DefaultPropertyAttribute("Content"))

        ' Apply the ReadOnlyAttribute to the Background property  
        ' of the Button class.
        builder.AddCustomAttributes( _
            GetType(Button), _
            "Background", _
            New ReadOnlyAttribute(True))

        Dim properties As PropertyDescriptorCollection = _
            TypeDescriptor.GetProperties(GetType(Button))
        Dim pd As PropertyDescriptor = properties("Foreground")
        builder.AddCustomAttributes( _
            GetType(Button), _
            pd, _
            New ReadOnlyAttribute(True))

        builder.AddCustomAttributes( _
            GetType(Button), _
            Button.WidthProperty, _
            New ReadOnlyAttribute(True))

        Dim members As MemberInfo() = GetType(Button).GetMember("Height")
        builder.AddCustomAttributes( _
            GetType(Button), _
            members(0), _
            New ReadOnlyAttribute(True))

        Dim attributes As AttributeTable = builder.CreateTable()

        MetadataStore.AddAttributeTable(attributes)

    End Sub 

End Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
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();

            builder.AddCustomAttributes(
                typeof(Button),
                new DefaultPropertyAttribute("Content"));

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

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

            builder.AddCustomAttributes(
                typeof(Button),
                Button.WidthProperty,
                new ReadOnlyAttribute(true)); 

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

            AttributeTable attributes = builder.CreateTable();

            MetadataStore.AddAttributeTable(attributes);
        }
    }
}

Inheritance Hierarchy

System.Object
  Microsoft.Windows.Design.Metadata.AttributeTableBuilder

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

AttributeTableBuilder Members

Microsoft.Windows.Design.Metadata Namespace

AttributeTable

AttributeCallbackBuilder

Other Resources

Metadata Store