Export (0) Print
Expand All

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.

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)

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Property, AllowMultiple = true)]
public sealed class NewItemTypesAttribute : Attribute

The NewItemTypesAttribute type exposes the following members.

  NameDescription
Public methodNewItemTypesAttribute(Type)Initializes a new instance of the NewItemTypesAttribute class.
Public methodNewItemTypesAttribute(Type[])Initializes a new instance of the NewItemTypesAttribute class.
Top

  NameDescription
Public propertyFactoryTypeGets or sets the factory type associated with this attribute.
Public propertyTypeIdGets the type ID for this attribute. (Overrides Attribute.TypeId.)
Public propertyTypesGets a list of Type objects that this attribute declares as being valid new item types.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves 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.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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 null, 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.

The following code example shows how to specify types and corresponding factories by using the NewItemTypesAttribute.


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;
            }
        }
    }
}


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

Community Additions

ADD
Show:
© 2014 Microsoft