Partager via


AttributeTable, classe

Mise à jour : novembre 2007

Table d'attributs de métadonnées pour définir l'apparence et le comportement au moment du design.

Espace de noms :  Microsoft.Windows.Design.Metadata
Assembly :  Microsoft.Windows.Design (dans Microsoft.Windows.Design.dll)

Syntaxe

Public NotInheritable Class AttributeTable

Dim instance As AttributeTable
public sealed class AttributeTable
public ref class AttributeTable sealed
public final class AttributeTable

Notes

Utilisez la classe AttributeTable pour associer des attributs de métadonnées au moment du design aux types Windows Presentation Foundation (WPF).

Une table d'attributs est essentiellement un dictionnaire en lecture seule, mais ses clés et valeurs sont calculées séparément. Il est plus efficace d'interroger une table d'attributs lorsqu'elle contient des attributs pour un type particulier. Le jeu réel d'attributs est créé à la demande.

Pour créer une table d'attributs, appelez la méthode CreateTable de la classe AttributeTableBuilder.

Exemples

L'exemple de code suivant indique comment créer et remplir une table d'attributs. Pour plus d'informations, consultez Procédure pas à pas : création d'un ornement au moment du design.

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
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()

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

        Dim attributes As AttributeTable = builder.CreateTable()

        MetadataStore.AddAttributeTable(attributes)

        Dim hasCustomAttributes As Boolean = attributes.ContainsAttributes(GetType(Button))

        Dim types As IEnumerable(Of Type) = attributes.AttributedTypes

        ' The following code shows how to retrieve custom attributes
        ' using the GetCustomAttributes method overloads.

        Dim attrs0 As IEnumerable = attributes.GetCustomAttributes(GetType(Button))

        Dim attrs1 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), "Background")

        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Button))
        Dim pd As PropertyDescriptor = properties("Background")
        Dim attrs2 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), pd)

        Dim attrs3 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), Button.BackgroundProperty)

        Dim members As MemberInfo() = GetType(Button).GetMembers()
        Dim i As Integer
        For i = 0 To members.Length
            If members(i).Name = "Background" Then
                Dim attrs4 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), members(i))
            End If
        Next i

    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();

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

            AttributeTable attributes = builder.CreateTable();

            MetadataStore.AddAttributeTable(attributes);

            bool hasCustomAttributes = attributes.ContainsAttributes(typeof(Button));

            IEnumerable<Type> types = attributes.AttributedTypes;

            // The following code shows how to retrieve custom attributes
            // using the GetCustomAttributes method overloads.

            IEnumerable attrs0 = attributes.GetCustomAttributes(typeof(Button));

            IEnumerable attrs1 = attributes.GetCustomAttributes(
                typeof(Button), 
                "Background");

            PropertyDescriptorCollection properties = 
                TypeDescriptor.GetProperties(typeof(Button));
            PropertyDescriptor pd = properties["Background"];
            IEnumerable attrs2 = attributes.GetCustomAttributes(typeof(Button), pd);

            IEnumerable attrs3 = attributes.GetCustomAttributes(
                typeof(Button),
                Button.BackgroundProperty);

            MemberInfo[] members = typeof(Button).GetMembers();
            for (int i = 0; i < members.Length; i++)
            {
                if (members[i].Name == "Background")
                {
                    IEnumerable attrs4 = attributes.GetCustomAttributes(
                        typeof(Button), 
                        members[i]);
                }
            }
        }
    }
}

Hiérarchie d'héritage

System.Object
  Microsoft.Windows.Design.Metadata.AttributeTable

Sécurité des threads

Tous les membres static (Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Voir aussi

Référence

Membres AttributeTable

Microsoft.Windows.Design.Metadata, espace de noms

AttributeTableBuilder

Autres ressources

Magasin de métadonnées