NotifyParentPropertyAttribute Classe

Definizione

Indica che la proprietà padre riceve una notifica quando il valore della proprietà associata a questo attributo viene modificato. La classe non può essere ereditata.

public ref class NotifyParentPropertyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class NotifyParentPropertyAttribute : Attribute
public sealed class NotifyParentPropertyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type NotifyParentPropertyAttribute = class
    inherit Attribute
type NotifyParentPropertyAttribute = class
    inherit Attribute
Public NotInheritable Class NotifyParentPropertyAttribute
Inherits Attribute
Ereditarietà
NotifyParentPropertyAttribute
Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come usare NotifyParentPropertyAttribute e la ExpandableObjectConverter classe per creare una proprietà espandibile in un controllo personalizzato.

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

Commenti

Applicare NotifyParentPropertyAttribute a una proprietà se la relativa proprietà padre deve ricevere una notifica delle modifiche ai valori della proprietà. Ad esempio, nella Finestra Proprietà la DataGridView.RowTemplate proprietà ha proprietà annidate, Height ad esempio e DefaultCellStyle. Queste proprietà nidificate sono contrassegnate con NotifyParentPropertyAttribute(true) in modo da notificare alla proprietà padre di aggiornarne il valore e visualizzarne la visualizzazione quando i valori della proprietà cambiano.

Per altre informazioni sull'uso degli attributi, vedere Attributi.

Costruttori

NotifyParentPropertyAttribute(Boolean)

Consente di inizializzare una nuova istanza della classe NotifyParentPropertyAttribute utilizzando il valore specificato per determinare se alla proprietà padre verranno notificate le modifiche apportate al valore della proprietà.

Campi

Default

Indica lo stato predefinito dell'attributo, ovvero che la proprietà non deve notificare alla proprietà padre le modifiche apportate al proprio valore. Questo campo è di sola lettura.

No

Indica che alla proprietà padre non devono essere notificate le modifiche apportate al valore della proprietà. Questo campo è di sola lettura.

Yes

Indica che alla proprietà padre vengono notificate le modifiche apportate al valore della proprietà. Questo campo è di sola lettura.

Proprietà

NotifyParent

Ottiene o imposta un valore che indica se alla proprietà padre devono essere notificate le modifiche apportate al valore della proprietà.

TypeId

Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.

(Ereditato da Attribute)

Metodi

Equals(Object)

Ottiene un valore che indica se l'oggetto specificato è uguale all'oggetto corrente.

GetHashCode()

Ottiene il codice hash di questo oggetto.

GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

Ottiene un valore che indica se il valore corrente dell'attributo è il valore predefinito dell'attributo stesso.

IsDefaultAttribute()

In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

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

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso a proprietà e metodi esposti da un oggetto.

(Ereditato da Attribute)

Si applica a

Vedi anche