Edit

Share via


NotifyPropertyChangedForAttribute Class

Definition

An attribute that can be used to support ObservablePropertyAttribute in generated properties. When this attribute is used, the generated property setter will also call OnPropertyChanged(String) (or the equivalent method in the target class) for the properties specified in the attribute data. This can be useful to keep the code compact when there are one or more dependent properties that should also be reported as updated when the value of the annotated observable property is changed. If this attribute is used on a property without ObservablePropertyAttribute, it is ignored.

In order to use this attribute, the containing type has to implement the INotifyPropertyChanged interface and expose a method with the same signature as OnPropertyChanged(String). If the containing type also implements the INotifyPropertyChanging interface and exposes a method with the same signature as OnPropertyChanging(String), then this method will be invoked as well by the property setter.

This attribute can be used as follows:

partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(FullName))]
    public partial string Name { get; set; }

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(FullName))]
    public partial string Surname { get; set; }

    public string FullName => $"{Name} {Surname}";
}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public partial string Name
    {
        get => field;
        set
        {
            if (!EqualityComparer<string>.Default.Equals(field, value))
            {
                OnPropertyChanging(nameof(Name));
                OnPropertyChanged(nameof(FullName));

                field = value;

                OnPropertyChanged(nameof(Name));
                OnPropertyChanged(nameof(FullName));
            }
        }
    }

    public partial string Surname
    {
        get => field;
        set
        {
            if (!EqualityComparer<string>.Default.Equals(field, value))
            {
                OnPropertyChanging(nameof(Surname));
                OnPropertyChanged(nameof(FullName));

                field = value;

                OnPropertyChanged(nameof(Surname));
                OnPropertyChanged(nameof(FullName));
            }
        }
    }
}
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public sealed class NotifyPropertyChangedForAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)>]
type NotifyPropertyChangedForAttribute = class
    inherit Attribute
Public NotInheritable Class NotifyPropertyChangedForAttribute
Inherits Attribute
Inheritance
NotifyPropertyChangedForAttribute
Attributes

Remarks

Just like ObservablePropertyAttribute, this attribute can also be used on fields as well.

Constructors

NotifyPropertyChangedForAttribute(String, String[])

Initializes a new instance of the NotifyPropertyChangedForAttribute class.

NotifyPropertyChangedForAttribute(String)

Initializes a new instance of the NotifyPropertyChangedForAttribute class.

Properties

PropertyNames

Gets the property names to also notify when the annotated property changes.

Applies to