NewItemTypesAttribute 类

用于指定哪种对象类型可被赋予属性值或属性类型值。

继承层次结构

System.Object
  System.Attribute
    Microsoft.Windows.Design.PropertyEditing.NewItemTypesAttribute

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

语法

声明
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Property, AllowMultiple := True)> _
Public NotInheritable Class NewItemTypesAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Property, AllowMultiple = true)]
public sealed class NewItemTypesAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Property, AllowMultiple = true)]
public ref class NewItemTypesAttribute sealed : public Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Property, AllowMultiple = true)>]
type NewItemTypesAttribute =  
    class
        inherit Attribute
    end
public final class NewItemTypesAttribute extends Attribute

NewItemTypesAttribute 类型公开以下成员。

构造函数

  名称 说明
公共方法 NewItemTypesAttribute(Type) 初始化 NewItemTypesAttribute 类的新实例。
公共方法 NewItemTypesAttribute(array<Type[]) 初始化 NewItemTypesAttribute 类的新实例。

页首

属性

  名称 说明
公共属性 FactoryType 获取或设置与此属性相关的工厂类型。
公共属性 TypeId 获取此特性的类型 ID。 (重写 Attribute.TypeId。)
公共属性 Types 获取一个 Type 对象的数组,此特性被声明为有效的新的项类型。

页首

方法

  名称 说明
公共方法 Equals 基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 返回此实例的哈希代码。 (继承自 Attribute。)
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
公共方法 IsDefaultAttribute 当在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute。)
公共方法 Match 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)

页首

显式接口实现

  名称 说明
显式接口实现私有方法 _Attribute.GetIDsOfNames 将一组名称映射为对应的一组调度标识符。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfo 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfoCount 检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.Invoke 提供对某一对象公开的属性和方法的访问。 (继承自 Attribute。)

页首

备注

使用 NewItemFactory 和 NewItemTypesAttribute 类展开类型列表中出现的要添加到集合编辑器中的项。

使用 NewItemTypesAttribute 将类型列表关联到属性。 通常,用户可从此类型列表进行选择。 如果属性值为 nullnull 引用(在 Visual Basic 中为 Nothing),则将向该属性分配所选类型的新实例。 一个新实例将添加到类型为集合的属性中。

如果有未指定的工厂,则属性窗口的添加项 UI 使用类型的名称填充可添加类型的列表。 类型的默认构造函数用于实例化选定的类型。

包含摘要类型的类型列表。 显示抽象类型后,必须指定工厂。

NewItemTypesAttribute 可在某个属性上多次使用,允许在某个属性上设置多余一个的工厂。 这对显示同一类型“加载项”列表中的不同项极为有用。 例如,一个工厂要添加红色背景的 MenuItem 并且另外一个工厂要添加背景为黑色的 MenuItem

如果属性表示集合,则 NewItemTypesAttribute 指定对象类型,根据此对象类型,可将实例创建为该集合中的项目。

示例

以下代码示例演示如何使用 NewItemTypesAttribute 来指定类型和对应工厂。

Imports System
Imports System.Collections
Imports System.Text
Imports System.Windows.Controls
Imports System.Windows
Imports Microsoft.Windows.Design.PropertyEditing
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class ControlWithCollectionProperty
    Inherits Button
    Private myCollMultipleTypesNoFactory As New ArrayList()
    Private myCollTypeWithFactory As New ArrayList()
    Private myCollTypeWithMultipleFactories As New ArrayList()
    Private myCollMultipleTypesWithFactory As New ArrayList()
    Private myCollMultipleTypesInvalid As New ArrayList()


    <NewItemTypesAttribute(GetType(Button), GetType(SolidColorBrush), GetType(Integer))>  _
    Public Property CollMultipleTypesNoFactory() As ArrayList 
        Get
            Return myCollMultipleTypesNoFactory
        End Get

        Set
            myCollMultipleTypesNoFactory = value
        End Set
    End Property


    <NewItemTypesAttribute(GetType(MyType), FactoryType := GetType(MyTypeFactory))>  _
    Public Property CollTypeWithFactory() As ArrayList 
        Get
            Return myCollTypeWithFactory
        End Get

        Set
            myCollTypeWithFactory = value
        End Set
    End Property


    <NewItemTypesAttribute(GetType(MyType), FactoryType := GetType(MyTypeAlternateFactory)), NewItemTypesAttribute(GetType(MyType), FactoryType := GetType(MyTypeFactory))>  _
    Public Property CollTypeWithMultipleFactories() As ArrayList 
        Get
            Return myCollTypeWithMultipleFactories
        End Get

        Set
            myCollTypeWithMultipleFactories = value
        End Set
    End Property

    ' The following code shows GetImage returning an 
    ' ImageSource, Image Control, and Rectangle.
    <NewItemTypesAttribute(GetType(MyType), GetType(Button), GetType(Brush), FactoryType := GetType(MyMultipleTypesFactory))>  _
    Public Property CollMultipleTypesWithFactory() As ArrayList 
        Get
            Return myCollMultipleTypesWithFactory
        End Get

        Set
            myCollMultipleTypesWithFactory = value
        End Set
    End Property

    ' The following case is not valid, because it contains
    ' a type that does not have a default constructor, and 
    ' no factory is specified.
    <NewItemTypesAttribute(GetType(Button), GetType(Brush), GetType(Size))>  _
    Public Property CollMultipleTypesInvalid() As ArrayList 
        Get
            Return myCollMultipleTypesInvalid
        End Get

        Set
            myCollMultipleTypesInvalid = value
        End Set
    End Property
End Class
using System;
using System.Collections;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using Microsoft.Windows.Design.PropertyEditing;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace PropertyEditingAttributeTestControls
{
    public class ControlWithCollectionProperty : Button
    {
        private ArrayList myCollMultipleTypesNoFactory = new ArrayList();
        private ArrayList myCollTypeWithFactory = new ArrayList();
        private ArrayList myCollTypeWithMultipleFactories = new ArrayList();
        private ArrayList myCollMultipleTypesWithFactory = new ArrayList();
        private ArrayList myCollMultipleTypesInvalid = new ArrayList();

        [NewItemTypesAttribute(typeof(Button), typeof(SolidColorBrush), typeof(int))]
        public ArrayList CollMultipleTypesNoFactory
        {
            get 
            { 
                return myCollMultipleTypesNoFactory; 
            }

            set 
            { 
                myCollMultipleTypesNoFactory = value;
            }
        }

        [NewItemTypesAttribute(typeof(MyType), FactoryType=typeof(MyTypeFactory))]
        public ArrayList CollTypeWithFactory
        {
            get 
            { 
                return myCollTypeWithFactory; 
            }

            set 
            { 
                myCollTypeWithFactory = value; 
            }
        }

        [NewItemTypesAttribute(
            typeof(MyType), 
            FactoryType = typeof(MyTypeAlternateFactory))]
        [NewItemTypesAttribute(
            typeof(MyType), 
            FactoryType = typeof(MyTypeFactory))]
        public ArrayList CollTypeWithMultipleFactories
        {
            get 
            { 
                return myCollTypeWithMultipleFactories; 
            }

            set 
            { 
                myCollTypeWithMultipleFactories = value; 
            }
        }

        // The following code shows GetImage returning an 
        // ImageSource, Image Control, and Rectangle.
        [NewItemTypesAttribute(
            typeof(MyType), 
            typeof(Button), 
            typeof(Brush), 
            FactoryType = typeof(MyMultipleTypesFactory))]
        public ArrayList CollMultipleTypesWithFactory
        {
            get 
            { 
                return myCollMultipleTypesWithFactory; 
            }

            set 
            { 
                myCollMultipleTypesWithFactory = value; 
            }
        }

        // The following case is not valid, because it contains
        // a type that does not have a default constructor, and 
        // no factory is specified.
        [NewItemTypesAttribute(typeof(Button), typeof(Brush), typeof(Size))]
        public ArrayList CollMultipleTypesInvalid
        {
            get 
            { 
                return myCollMultipleTypesInvalid; 
            }

            set 
            { 
                myCollMultipleTypesInvalid = value;
            }
        }
    }
}

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参见

参考

Microsoft.Windows.Design.PropertyEditing 命名空间

NewItemFactory

其他资源

属性编辑体系结构

WPF 设计器扩展性