Table of Contents

Class NotifyPropertyChangedForAttribute

Namespace
CommunityToolkit.Mvvm.ComponentModel
Assembly
CommunityToolkit.Mvvm.dll

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 in a field 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))]
    private string name;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
private string surname;

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

}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public string Name
    {
        get => name;
        set
        {
            if (SetProperty(ref name, value))
            {
                OnPropertyChanged(nameof(FullName));
            }
        }
    }
public string Surname
{
    get => surname;
    set
    {
        if (SetProperty(ref surname, value))
        {
            OnPropertyChanged(nameof(FullName));
        }
    }
}

}

[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class NotifyPropertyChangedForAttribute : Attribute
Inheritance
NotifyPropertyChangedForAttribute
Inherited Members

Constructors

NotifyPropertyChangedForAttribute(string)

Initializes a new instance of the NotifyPropertyChangedForAttribute class.

public NotifyPropertyChangedForAttribute(string propertyName)

Parameters

propertyName string

The name of the property to also notify when the annotated property changes.

NotifyPropertyChangedForAttribute(string, params string[])

Initializes a new instance of the NotifyPropertyChangedForAttribute class.

public NotifyPropertyChangedForAttribute(string propertyName, params string[] otherPropertyNames)

Parameters

propertyName string

The name of the property to also notify when the annotated property changes.

otherPropertyNames string[]

The other property names to also notify when the annotated property changes. This parameter can optionally be used to indicate a series of dependent properties from the same attribute, to keep the code more compact.

Properties

PropertyNames

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

public string[] PropertyNames { get; }

Property Value

string[]