NewItemTypesAttribute (Clase)

Actualización: noviembre 2007

Se utiliza para especificar los tipos de objeto que se pueden asignar como valor de una propiedad o como valor de un tipo de propiedad.

Espacio de nombres:  Microsoft.Windows.Design.PropertyEditing
Ensamblado:  Microsoft.Windows.Design (en Microsoft.Windows.Design.dll)

Sintaxis

<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

Comentarios

Use las clases NewItemFactory y NewItemTypesAttribute para extender los elementos que aparecen en una lista de los tipos que se van a agregar a un editor de colecciones.

Use NewItemTypesAttribute para asociar una lista de tipos a una propiedad. Normalmente, el usuario puede seleccionar tipos de esta lista. Si el valor de la propiedad es nullreferencia null (Nothing en Visual Basic), se asigna una nueva instancia del tipo seleccionado a la propiedad. Se agrega una nueva instancia a una propiedad cuyo tipo es una colección.

Si no se especifica un generador, la interfaz “Agregar elemento” de la ventana Propiedades utiliza el nombre del tipo para rellenar la lista de los tipos que se pueden agregar. Se utiliza el constructor predeterminado del tipo para crear una instancia del tipo seleccionado.

La lista de tipos puede contener tipos abstractos. Cuando hay un tipo abstracto en la lista, se debe especificar un generador.

NewItemTypesAttribute puede usarse varias veces en una propiedad, lo que permite establecer varios generadores en una propiedad. Esto es útil para mostrar diferentes elementos en la lista de “Agregar elemento” para el mismo tipo. Por ejemplo, un generador podría agregar un control MenuItem con un fondo de color rojo y otro podría agregar un control MenuItem con un fondo de color negro.

Si la propiedad representa una colección, NewItemTypesAttribute especifica los tipos de objeto de los que se pueden crear instancias como elementos de esa colección.

Ejemplos

En el ejemplo de código siguiente se muestra cómo especificar los tipos y los correspondientes generadores mediante 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;
            }
        }
    }
}

Jerarquía de herencia

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

Seguridad para subprocesos

Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Vea también

Referencia

NewItemTypesAttribute (Miembros)

Microsoft.Windows.Design.PropertyEditing (Espacio de nombres)

NewItemFactory

Otros recursos

Arquitectura de edición de propiedades

Extensibilidad de WPF Designer