NotifyParentPropertyAttribute Klasa

Definicja

Wskazuje, że właściwość nadrzędna jest powiadamiana o zmodyfikowaniu wartości właściwości, do której zastosowano ten atrybut. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
NotifyParentPropertyAttribute
Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą NotifyParentPropertyAttribute klasy i ExpandableObjectConverter utworzyć właściwość rozszerzalną w kontrolce niestandardowej.

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

Uwagi

Zastosuj NotifyParentPropertyAttribute do właściwości, jeśli jej właściwość nadrzędna powinna otrzymywać powiadomienia o zmianach wartości właściwości. Na przykład w okno Właściwości DataGridView.RowTemplate właściwość ma zagnieżdżone właściwości, takie jak Height i DefaultCellStyle. Te zagnieżdżone właściwości są oznaczone za pomocą NotifyParentPropertyAttribute(true) polecenia , dzięki czemu powiadamiają właściwość nadrzędną o zaktualizowaniu jej wartości i wyświetleniu, gdy wartości właściwości się zmieniają.

Aby uzyskać więcej informacji na temat używania atrybutów, zobacz Atrybuty.

Konstruktory

NotifyParentPropertyAttribute(Boolean)

Inicjuje nowe wystąpienie NotifyParentPropertyAttribute klasy przy użyciu określonej wartości w celu określenia, czy właściwość nadrzędna jest powiadamiana o zmianach wartości właściwości.

Pola

Default

Wskazuje domyślny stan atrybutu, że właściwość nie powinna powiadamiać właściwości nadrzędnej o zmianach jego wartości. To pole jest tylko do odczytu.

No

Wskazuje, że właściwość nadrzędna nie jest powiadamiana o zmianach wartości właściwości. To pole jest tylko do odczytu.

Yes

Wskazuje, że właściwość nadrzędna jest powiadamiana o zmianach wartości właściwości. To pole jest tylko do odczytu.

Właściwości

NotifyParent

Pobiera lub ustawia wartość wskazującą, czy właściwość nadrzędna powinna być powiadamiana o zmianach wartości właściwości.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego elementu Attribute.

(Odziedziczone po Attribute)

Metody

Equals(Object)

Pobiera wartość wskazującą, czy określony obiekt jest taki sam jak bieżący obiekt.

GetHashCode()

Pobiera kod skrótu dla tego obiektu.

GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
IsDefaultAttribute()

Pobiera wartość wskazującą, czy bieżąca wartość atrybutu jest wartością domyślną atrybutu.

IsDefaultAttribute()

W przypadku zastąpienia w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

W przypadku zastąpienia w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie obiektu, którego można użyć do pobrania informacji o typie interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też