NotifyParentPropertyAttribute Osztály

Definíció

Azt jelzi, hogy a szülőtulajdonság értesítést kap az attribútum által alkalmazott tulajdonság értékének módosításakor. Ez az osztály nem örökölhető.

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
Öröklődés
NotifyParentPropertyAttribute
Attribútumok

Példák

Az alábbi példakód bemutatja, hogyan hozhat létre bővíthető tulajdonságot egy egyéni vezérlőn az osztály és az NotifyParentPropertyAttributeExpandableObjectConverter osztály használatával.

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

namespace ExpandableObjectDemo;

public partial class DemoControl : UserControl
{
    Container components;

    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; set; } = new();

    void InitializeComponent()
    {
        components = new Container();
        AutoScaleMode = AutoScaleMode.Font;
    }
}

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

    [Browsable(true),
    NotifyParentProperty(true),
    EditorBrowsable(EditorBrowsableState.Always),
    DefaultValue(1)]
    public int BorderSize
    {
        get => 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 => 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) => destinationType == typeof(string)
            ? ""
            : 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

Megjegyzések

Alkalmazható NotifyParentPropertyAttribute egy tulajdonságra, ha a szülőtulajdonság értesítést kap a tulajdonság értékeinek változásairól. A Tulajdonságok ablak például a DataGridView.RowTemplate tulajdonság beágyazott tulajdonságokkal rendelkezik, például Height és DefaultCellStyle. Ezeket a beágyazott tulajdonságokat a rendszer megjelöli NotifyParentPropertyAttribute(true) , így értesítik a szülőtulajdonságot, hogy frissítse az értékét, és megjelenjen a tulajdonságértékek változásakor.

Az attribútumok használatáról további információt az Attribútumok című témakörben talál.

Konstruktorok

Name Description
NotifyParentPropertyAttribute(Boolean)

Inicializálja az NotifyParentPropertyAttribute osztály új példányát a megadott értékkel annak megállapításához, hogy a szülőtulajdonság értesítést kap-e a tulajdonság értékének változásairól.

Mezők

Name Description
Default

Az alapértelmezett attribútum állapotát jelzi, hogy a tulajdonság ne értesítse a szülőtulajdonságot az értékében bekövetkezett változásokról. Ez a mező csak olvasható.

No

Azt jelzi, hogy a szülőtulajdonság nem kap értesítést a tulajdonság értékének változásairól. Ez a mező csak olvasható.

Yes

Azt jelzi, hogy a szülőtulajdonság értesítést kap a tulajdonság értékének változásairól. Ez a mező csak olvasható.

Tulajdonságok

Name Description
NotifyParent

Beolvas vagy beállít egy értéket, amely jelzi, hogy a szülőtulajdonság értesítést kapjon-e a tulajdonság értékének változásairól.

TypeId

Ha származtatott osztályban implementálják, ehhez egy egyedi azonosítót Attributekap.

(Öröklődés forrása Attribute)

Metódusok

Name Description
Equals(Object)

Beolvas egy értéket, amely jelzi, hogy a megadott objektum megegyezik-e az aktuális objektummal.

GetHashCode()

Lekéri az objektum kivonatkódját.

GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
IsDefaultAttribute()

Beolvas egy értéket, amely jelzi, hogy az attribútum aktuális értéke-e az attribútum alapértelmezett értéke.

Match(Object)

Származtatott osztály felülírásakor egy olyan értéket ad vissza, amely jelzi, hogy ez a példány egy adott objektummal egyenlő-e.

(Öröklődés forrása Attribute)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

Explicit interfész-implementációk

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

Névkészletet képez le a küldési azonosítók megfelelő készletére.

(Öröklődés forrása Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Lekéri egy objektum típusadatait, amelyek a felület típusadatainak lekérésére használhatók.

(Öröklődés forrása Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Lekéri az objektumok által biztosított típusinformációs felületek számát (0 vagy 1).

(Öröklődés forrása Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Hozzáférést biztosít az objektumok által közzétett tulajdonságokhoz és metódusokhoz.

(Öröklődés forrása Attribute)

A következőre érvényes:

Lásd még