AttributeCallback 委托

更新:2007 年 11 月

在某一类型需要属性时调用。

命名空间:  Microsoft.Windows.Design.Metadata
程序集:  Microsoft.Windows.Design(在 Microsoft.Windows.Design.dll 中)

语法

声明
Public Delegate Sub AttributeCallback ( _
    builder As AttributeCallbackBuilder _
)
用法
Dim instance As New AttributeCallback(AddressOf HandlerMethod)
public delegate void AttributeCallback(
    AttributeCallbackBuilder builder
)
public delegate void AttributeCallback(
    AttributeCallbackBuilder^ builder
)
JScript 不支持委托。

参数

备注

在填充较大的属性表时,可以使用 AttributeCallback 委托。通过使用回调模式,可将属性表填充一直推迟到设计器需要某一类型的设计时元数据时。

示例

下面的代码示例演示如何使用 AttributeCallbackBuilder 类创建和填充属性表。

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

另请参见

参考

Microsoft.Windows.Design.Metadata 命名空间

AttributeTableBuilder

AddCallback

AttributeTable

其他资源

元数据存储区

演练:创建设计时装饰器

了解 WPF 设计器扩展性