Udostępnij za pośrednictwem


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.

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Property, AllowMultiple := True)> _
Public NotInheritable Class NewItemTypesAttribute _
    Inherits Attribute
'Usage
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

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

Inheritance Hierarchy

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

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

NewItemTypesAttribute Members

Microsoft.Windows.Design.PropertyEditing Namespace

NewItemFactory

Other Resources

Property Editing Architecture

WPF Designer Extensibility