NotifyParentPropertyAttribute NotifyParentPropertyAttribute NotifyParentPropertyAttribute NotifyParentPropertyAttribute Class

Definición

Indica que la propiedad principal se notifica cuando cambia el valor de la propiedad a la que se aplica este atributo. Indicates that the parent property is notified when the value of the property that this attribute is applied to is modified. Esta clase no puede heredarse. This class cannot be inherited.

public ref class NotifyParentPropertyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class NotifyParentPropertyAttribute : Attribute
type NotifyParentPropertyAttribute = class
    inherit Attribute
Public NotInheritable Class NotifyParentPropertyAttribute
Inherits Attribute
Herencia
NotifyParentPropertyAttributeNotifyParentPropertyAttributeNotifyParentPropertyAttributeNotifyParentPropertyAttribute
Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el NotifyParentPropertyAttribute y ExpandableObjectConverter clase para crear una propiedad expansible en un control personalizado.The following code example demonstrates how to use the NotifyParentPropertyAttribute and the ExpandableObjectConverter class to create an expandable property on a custom control.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace ExpandableObjectDemo
{
    public partial class DemoControl : UserControl
    {
        BorderAppearance borderAppearanceValue = new BorderAppearance();
        private System.ComponentModel.IContainer components = null;

        public DemoControl()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Category("Demo")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public BorderAppearance Border
        {
            get
            {
                return this.borderAppearanceValue;
            }

            set
            {
                this.borderAppearanceValue = value;
            }
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }
    }

    [TypeConverter(typeof(BorderAppearanceConverter))]
    public class BorderAppearance
    {
        private int borderSizeValue = 1;
        private Color borderColorValue = Color.Empty;

        [Browsable(true),
        NotifyParentProperty(true),
        EditorBrowsable(EditorBrowsableState.Always),
        DefaultValue(1)]
        public int BorderSize
        {
            get
            {
                return borderSizeValue;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException(
                        "BorderSize",
                        value,
                        "must be >= 0");
                }

                if (borderSizeValue != value)
                {
                    borderSizeValue = value;
                }
            }
        }

        [Browsable(true)]
        [NotifyParentProperty(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DefaultValue(typeof(Color), "")]
        public Color BorderColor
        {
            get
            {
                return borderColorValue;
            }
            set
            {
                if (value.Equals(Color.Transparent))
                {
                    throw new NotSupportedException("Transparent colors are not supported.");
                }

                if (borderColorValue != value)
                {
                    borderColorValue = value;
                }
            }
        }
    }

    public class BorderAppearanceConverter : ExpandableObjectConverter
    {
        // This override prevents the PropertyGrid from 
        // displaying the full type name in the value cell.
        public override object ConvertTo(
            ITypeDescriptorContext context,
            CultureInfo culture,
            object value,
            Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return "";
            }

            return base.ConvertTo(
                context,
                culture,
                value,
                destinationType);
        }
    }
}
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Globalization
Imports System.Windows.Forms

Public Class DemoControl
    Inherits UserControl

    Private borderAppearanceValue As New BorderAppearance()
    Private components As System.ComponentModel.IContainer = Nothing


    Public Sub New()
        InitializeComponent()

    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If

        MyBase.Dispose(disposing)

    End Sub

    <Browsable(True), _
    EditorBrowsable(EditorBrowsableState.Always), _
    Category("Demo"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property Border() As BorderAppearance

        Get
            Return Me.borderAppearanceValue
        End Get

        Set(ByVal value As BorderAppearance)
            Me.borderAppearanceValue = value
        End Set

    End Property

    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

    End Sub
End Class

<TypeConverter(GetType(BorderAppearanceConverter))>  _
Public Class BorderAppearance
    Private borderSizeValue As Integer = 1
    Private borderColorValue As Color = Color.Empty
    
    
    <Browsable(True), NotifyParentProperty(True), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(1)>  _
    Public Property BorderSize() As Integer 
        Get
            Return borderSizeValue
        End Get
        Set
            If value < 0 Then
                Throw New ArgumentOutOfRangeException("BorderSize", value, "must be >= 0")
            End If
            
            If borderSizeValue <> value Then
                borderSizeValue = value
            End If
        End Set
    End Property
    
    
    <Browsable(True), NotifyParentProperty(True), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(GetType(Color), "")>  _
    Public Property BorderColor() As Color 
        Get
            Return borderColorValue
        End Get
        Set
            If value.Equals(Color.Transparent) Then
                Throw New NotSupportedException("Transparent colors are not supported.")
            End If
            
            If borderColorValue <> value Then
                borderColorValue = value
            End If
        End Set
    End Property
End Class

Public Class BorderAppearanceConverter
    Inherits ExpandableObjectConverter
    
    ' This override prevents the PropertyGrid from 
    ' displaying the full type name in the value cell.
    Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object

        If destinationType Is GetType(String) Then
            Return ""
        End If

        Return MyBase.ConvertTo(context, culture, value, destinationType)

    End Function
End Class

Comentarios

Aplicar NotifyParentPropertyAttribute a una propiedad si su propiedad primaria debe recibir una notificación de cambios en los valores de propiedad.Apply NotifyParentPropertyAttribute to a property if its parent property should receive notification of changes to the property's values. Por ejemplo, en la ventana Propiedades, el DataGridView.RowTemplate propiedad anidadas como propiedades Height y DefaultCellStyle.For example, in the Properties window, the DataGridView.RowTemplate property has nested properties such as Height and DefaultCellStyle. Estas propiedades anidadas se marcan con NotifyParentPropertyAttribute(true) para que notifiquen a la propiedad parent para actualizar su valor y mostrar cuando cambian los valores de la propiedad.These nested properties are marked with NotifyParentPropertyAttribute(true) so they notify the parent property to update its value and display when the property values change.

Para obtener más información sobre el uso de atributos, vea atributos.For more information about using attributes, see Attributes.

Constructores

NotifyParentPropertyAttribute(Boolean) NotifyParentPropertyAttribute(Boolean) NotifyParentPropertyAttribute(Boolean) NotifyParentPropertyAttribute(Boolean)

Inicializa una nueva instancia de la clase NotifyParentPropertyAttribute utilizando el valor especificado para determinar si a la propiedad principal se le notifican los cambios en el valor de la propiedad. Initializes a new instance of the NotifyParentPropertyAttribute class, using the specified value to determine whether the parent property is notified of changes to the value of the property.

Campos

Default Default Default Default

Indica el estado de atributo predeterminado, que indica que la propiedad principal no recibirá notificación de los cambios en su valor. Indicates the default attribute state, that the property should not notify the parent property of changes to its value. Este campo es de sólo lectura. This field is read-only.

No No No No

Indica que a la propiedad principal no se le notifican los cambios en el valor de la propiedad. Indicates that the parent property is not be notified of changes to the value of the property. Este campo es de sólo lectura. This field is read-only.

Yes Yes Yes Yes

Indica que a la propiedad principal se le notifican los cambios en el valor de la propiedad. Indicates that the parent property is notified of changes to the value of the property. Este campo es de sólo lectura. This field is read-only.

Propiedades

NotifyParent NotifyParent NotifyParent NotifyParent

Obtiene o establece un valor que indica si a la propiedad principal se le deben notificar los cambios en el valor de la propiedad. Gets or sets a value indicating whether the parent property should be notified of changes to the value of the property.

TypeId TypeId TypeId TypeId

Cuando se implementa en una clase derivada, obtiene un identificador único para este Attribute. When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Métodos

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Obtiene un valor que indica si el objeto especificado es el mismo que el objeto actual. Gets a value indicating whether the specified object is the same as the current object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Obtiene el código hash de este objeto. Gets the hash code for this object.

GetType() GetType() GetType() GetType()

Obtiene el Type de la instancia actual. Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

Obtiene un valor que indica si el valor actual del atributo es su valor predeterminado. Gets a value indicating whether the current value of the attribute is the default value for the attribute.

Match(Object) Match(Object) Match(Object) Match(Object)

Cuando se invalida en una clase derivada, devuelve un valor que indica si esta instancia es igual a un objeto especificado. When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficial del Object actual. Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Devuelve una cadena que representa el objeto actual. Returns a string that represents the current object.

(Inherited from Object)

Implementaciones de interfaz explícitas

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Asigna un conjunto de nombres a un conjunto correspondiente de identificadores de envío. Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Obtiene la información de tipos de un objeto, que puede utilizarse para obtener la información de tipos de una interfaz. Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

Recupera el número de interfaces de información de tipo que proporciona un objeto (0 ó 1). Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Proporciona acceso a las propiedades y los métodos expuestos por un objeto. Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Se aplica a

Consultar también