Edit

Share via


ObservablePropertyAttribute Class

Definition

An attribute that indicates that a given field should be wrapped by a generated observable property. 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]
    private string name;

    [ObservableProperty]
    private bool isEnabled;
}

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

    public bool IsEnabled
    {
        get => name;
        set => SetProperty(ref isEnabled, value);
    }
}
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=false, Inherited=false)]
public sealed class ObservablePropertyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=false, Inherited=false)>]
type ObservablePropertyAttribute = class
    inherit Attribute
Public NotInheritable Class ObservablePropertyAttribute
Inherits Attribute
Inheritance
ObservablePropertyAttribute
Attributes

Remarks

The generated properties will automatically use the UpperCamelCase format for their names, which will be derived from the field names. The generator can also recognize fields using either the _lowerCamel or m_lowerCamel naming scheme. Otherwise, the first character in the source field name will be converted to uppercase (eg. isEnabled to IsEnabled).

Constructors

ObservablePropertyAttribute()

Applies to