NewItemTypesAttribute 类
更新:2007 年 11 月
用于指定哪些对象类型可以作为属性值或作为属性类型的值进行分配。
命名空间: Microsoft.Windows.Design.PropertyEditing
程序集: Microsoft.Windows.Design(在 Microsoft.Windows.Design.dll 中)
语法
声明
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Property, AllowMultiple := True)> _
Public NotInheritable Class NewItemTypesAttribute _
Inherits Attribute
用法
Dim instance As NewItemTypesAttribute
[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
public final class NewItemTypesAttribute extends 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;
}
}
}
}
继承层次结构
System.Object
System.Attribute
Microsoft.Windows.Design.PropertyEditing.NewItemTypesAttribute
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
另请参见
参考
Microsoft.Windows.Design.PropertyEditing 命名空间