共用方式為


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),就會將所選型別的新執行個體指定給屬性。新執行個體會加入至其型別為集合的屬性。

如果未指定 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;
            }
        }
    }
}

繼承階層架構

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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

NewItemTypesAttribute 成員

Microsoft.Windows.Design.PropertyEditing 命名空間

NewItemFactory

其他資源

屬性編輯架構

WPF 設計工具擴充性