Share via


NewItemTypesAttribute Class

Used to specify which object types can be assigned as the value of a property or as the value of a property type.

Inheritance Hierarchy

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

Namespace:  Microsoft.Windows.Design.PropertyEditing
Assembly:  Microsoft.Windows.Design.Interaction (in Microsoft.Windows.Design.Interaction.dll)

Syntax

'Declaration
<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

The NewItemTypesAttribute type exposes the following members.

Constructors

  Name Description
Public method NewItemTypesAttribute(Type) Initializes a new instance of the NewItemTypesAttribute class.
Public method NewItemTypesAttribute(array<Type[]) Initializes a new instance of the NewItemTypesAttribute class.

Top

Properties

  Name Description
Public property FactoryType Gets or sets the factory type associated with this attribute.
Public property TypeId Gets the type ID for this attribute. (Overrides Attribute.TypeId.)
Public property Types Gets a list of Type objects that this attribute declares as being valid new item types.

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttribute When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method _Attribute.GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfo Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)

Top

Remarks

Use the NewItemFactory and NewItemTypesAttribute classes to extend the items that appear in a list of types to be added in a collection editor.

Use NewItemTypesAttribute to associate a list of types to a property. Typically, the user can select from this list of types. If the property value is nulla null reference (Nothing in Visual Basic), a new instance of the selected type is assigned to the property. A new instance is added to a property whose type is a collection.

If a factory is not specified, the Properties window's "add item" UI uses the name of the type to populate the list of types that can be added. The type's default constructor is used to instantiate the type that is selected.

The list of types can contain abstract types. When an abstract type is listed, a factory must be specified.

NewItemTypesAttribute can be used multiple times on a property, which allows setting more than one factory on a property. This is useful for showing different items in the "add item" list for the same type. For example, one factory could add a MenuItem with a red background and another could add a MenuItem with a black background.

If the property represents a collection, the NewItemTypesAttribute specifies the object types for which instances can be created as items in that collection.

Examples

The following code example shows how to specify types and corresponding factories by using the 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;
            }
        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Windows.Design.PropertyEditing Namespace

NewItemFactory

Other Resources

Property Editing Architecture

WPF Designer Extensibility