AlsoNotifyChangeForAttribute Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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]
[AlsoNotifyChangeFor(nameof(FullName))]
private string name;
[ObservableProperty]
[AlsoNotifyChangeFor(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));
}
}
}
}
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=true, Inherited=false)]
public sealed class AlsoNotifyChangeForAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=true, Inherited=false)>]
type AlsoNotifyChangeForAttribute = class
inherit Attribute
Public NotInheritable Class AlsoNotifyChangeForAttribute
Inherits Attribute
- Inheritance
-
AlsoNotifyChangeForAttribute
- Attributes
Constructors
AlsoNotifyChangeForAttribute(String, String[]) |
Initializes a new instance of the AlsoNotifyChangeForAttribute class. |
AlsoNotifyChangeForAttribute(String) |
Initializes a new instance of the AlsoNotifyChangeForAttribute class. |
Properties
PropertyNames |
Gets the property names to also notify when the annotated property changes. |