用來指定哪些物件型別可以指定為屬性的值或指定為屬性型別的值。
繼承階層架構
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 | 取得或設定與這個屬性相關聯的 Factory 型別。 |
![]() |
TypeId | 取得這個屬性的型別 ID。 (覆寫 Attribute.TypeId。) |
![]() |
Types | 取得 Type 物件清單,而這個屬性會將這些物件宣告為有效的新項目型別。 |
回頁首
方法
| 名稱 | 說明 | |
|---|---|---|
![]() |
Equals | 基礎架構。傳回數值,表示這個執行個體是否等於指定的物件。 (繼承自 Attribute)。 |
![]() |
Finalize | 允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。 (繼承自 Object)。 |
![]() |
GetHashCode | 傳回這個執行個體的雜湊程式碼。 (繼承自 Attribute)。 |
![]() |
GetType | 取得目前執行個體的 Type。 (繼承自 Object)。 |
![]() |
IsDefaultAttribute | 在衍生類別中覆寫時,表示這個執行個體的值是否為衍生類別的預設值。 (繼承自 Attribute)。 |
![]() |
Match | 在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。 (繼承自 Attribute)。 |
![]() |
MemberwiseClone | 建立目前 Object 的淺層複本 (Shallow Copy)。 (繼承自 Object)。 |
![]() |
ToString | 傳回表示目前物件的字串。 (繼承自 Object)。 |
回頁首
明確介面實作
| 名稱 | 說明 | |
|---|---|---|
![]() ![]() |
_Attribute.GetIDsOfNames | 將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承自 Attribute)。 |
![]() ![]() |
_Attribute.GetTypeInfo | 擷取物件的型別資訊,可以用來取得介面的型別資訊。 (繼承自 Attribute)。 |
![]() ![]() |
_Attribute.GetTypeInfoCount | 擷取物件提供的型別資訊介面數目 (0 或 1)。 (繼承自 Attribute)。 |
![]() ![]() |
_Attribute.Invoke | 提供物件所公開的屬性和方法的存取權。 (繼承自 Attribute)。 |
回頁首
備註
使用 NewItemFactory 和 NewItemTypesAttribute 類別,擴充顯示於型別清單而要加入集合編輯器中的項目。
使用 NewItemTypesAttribute,使型別清單與屬性產生關聯。 使用者通常可以從這份型別清單進行選取。 如果屬性值為 nullNull 參照 (即 Visual Basic 中的 Nothing),就會將所選型別的新執行個體指定給屬性。 新執行個體會加入至其型別為集合的屬性。
如果未指定 Factory,[屬性] 視窗的 [加入項目] UI 就會使用型別名稱,填入可以加入的型別清單。 型別的預設建構函式會用於具現化所選取的型別。
型別清單可以包含抽象型別。 列出抽象型別時,必須指定 Factory。
NewItemTypesAttribute 可以多次用於某個屬性,如此即可在某個屬性上設定多個 Factory。 這很適合用於在相同型別的 [加入項目] 清單中顯示不同的項目。 例如,一個 Factory 可以加入具有紅色背景的 MenuItem,而另一個 Factory 則可加入具有黑色背景的 MenuItem。
如果屬性表示集合,則 NewItemTypesAttribute 會指定物件型別,而針對這些物件型別將執行個體建立為該集合中的項目。
範例
下列程式碼範例顯示如何使用 NewItemTypesAttribute,指定型別與對應 Factory。
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;
}
}
}
}
執行緒安全
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
請參閱
參考
Microsoft.Windows.Design.PropertyEditing 命名空間
.gif)
.gif)
.gif)
.gif)
.gif)