Namespace CommunityToolkit.Mvvm.ComponentModel
Classes
- INotifyPropertyChangedAttribute
An attribute that indicates that a given type should implement the INotifyPropertyChanged interface and have minimal built-in functionality to support it. This includes exposing the necessary event and having two methods to raise it that mirror OnPropertyChanged(PropertyChangedEventArgs) and OnPropertyChanged(string?). For more extensive support, use ObservableObjectAttribute.
This attribute can be used as follows:
[INotifyPropertyChanged] partial class MyViewModel : SomeOtherClass { // Other members here... }
- NotifyCanExecuteChangedForAttribute
An attribute that can be used to support IRelayCommand properties in generated properties. When this attribute is used, the generated property setter will also call NotifyCanExecuteChanged() for the properties specified in the attribute data, causing the validation logic for the command to be executed again. This can be useful to keep the code compact when there are one or more dependent commands that should also be notified when a property is updated. If this attribute is used in a field without ObservablePropertyAttribute, it is ignored (just like NotifyPropertyChangedForAttribute).
In order to use this attribute, the target property has to implement the IRelayCommand interface.
This attribute can be used as follows:
And with this, code analogous to this will be generated:partial class MyViewModel : ObservableObject { [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(GreetUserCommand))] private string name;
public IRelayCommand GreetUserCommand { get; }
}
partial class MyViewModel { public string Name { get => name; set { if (SetProperty(ref name, value)) { GreetUserCommand.NotifyCanExecuteChanged(); } } } }
- NotifyDataErrorInfoAttribute
An attribute that can be used to support ObservablePropertyAttribute in generated properties, when applied to fields contained in a type that is inheriting from ObservableValidator and using any validation attributes. When this attribute is used, the generated property setter will also call ValidateProperty(object?, string). This allows generated properties to opt-in into validation behavior without having to fallback into a full explicit observable property.
This attribute can be used as follows:
And with this, code analogous to this will be generated:partial class MyViewModel : ObservableValidator { [ObservableProperty] [NotifyDataErrorInfo] [Required] [MinLength(2)] private string username; }
partial class MyViewModel { [Required] [MinLength(2)] public string Username { get => username; set => SetProperty(ref username, value, validate: true); } }
- NotifyPropertyChangedForAttribute
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:
And with this, code analogous to this will be generated:partial class MyViewModel : ObservableObject { [ObservableProperty] [NotifyPropertyChangedFor(nameof(FullName))] private string name;
[ObservableProperty] [NotifyPropertyChangedFor(nameof(FullName))] private string surname; public string FullName => $"{Name} {Surname}";
}
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)); } } }
}
- NotifyPropertyChangedRecipientsAttribute
An attribute that can be used to support ObservablePropertyAttribute in generated properties, when applied to fields contained in a type that is either inheriting from ObservableRecipient, or annotated with ObservableRecipientAttribute. When this attribute is used, the generated property setter will also call Broadcast<T>(T, T, string?). This allows generated properties to opt-in into broadcasting behavior without having to fallback into a full explicit observable property.
This attribute can be used as follows:
partial class MyViewModel : ObservableRecipient { [ObservableProperty] [NotifyPropertyChangedRecipients] private string username; }
And with this, code analogous to this will be generated:
partial class MyViewModel { public string Username { get => username; set => SetProperty(ref username, value, broadcast: true); } }
This attribute can also be added to a class, and if so it will affect all generated properties in that type and inherited types.
- ObservableObject
A base class for objects of which the properties must be observable.
- ObservableObject.TaskNotifier
A wrapping class that can hold a Task value.
- ObservableObject.TaskNotifier<T>
A wrapping class that can hold a Task<TResult> value.
- ObservableObjectAttribute
An attribute that indicates that a given type should have all the members from ObservableObject generated into it, as well as the INotifyPropertyChanged and INotifyPropertyChanging interfaces. This can be useful when you want the same functionality from ObservableObject into a class that already inherits from another one (since C# doesn't support multiple inheritance). This attribute will trigger the source generator to just create the same APIs directly into the decorated class.
This attribute can be used as follows:
And with this, the same APIs from ObservableObject will be available on this type as well.[ObservableObject] partial class MyViewModel : SomeOtherClass { // Other members here... }
- ObservablePropertyAttribute
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 inherit from ObservableObject, or it must be using ObservableObjectAttribute or INotifyPropertyChangedAttribute. If the containing type also implements the INotifyPropertyChanging (that is, if it either inherits from ObservableObject or is using ObservableObjectAttribute), then the generated code will also invoke OnPropertyChanging(PropertyChangingEventArgs) to signal that event.
This attribute can be used as follows:
And with this, code analogous to this will be generated:partial class MyViewModel : ObservableObject { [ObservableProperty] private string name;
[ObservableProperty] private bool isEnabled;
}
partial class MyViewModel { public string Name { get => name; set => SetProperty(ref name, value); }
public bool IsEnabled { get => isEnabled; set => SetProperty(ref isEnabled, value); }
}
- ObservableRecipient
A base class for observable objects that also acts as recipients for messages. This class is an extension of ObservableObject which also provides built-in support to use the IMessenger type.
- ObservableRecipientAttribute
An attribute that indicates that a given type should have all the members from ObservableRecipient generated into it. This can be useful when you want the same functionality from ObservableRecipient into a class that already inherits from another one (since C# doesn't support multiple inheritance). This attribute will trigger the source generator to just create the same APIs directly into the decorated class. For instance, this attribute can be used to easily combine the functionality from both ObservableValidator and ObservableRecipient, by using ObservableValidator as the base class and adding this attribute to the declared type.
This attribute can be used as follows:
And with this, the same APIs from ObservableRecipient will be available on this type as well.[ObservableRecipient] partial class MyViewModel : ObservableValidator { // Other members here... }
To avoid conflicts with other APIs in types where the new members are being generated, constructors are only generated when the annotated type doesn't have any explicit constructors being declared. If that is the case, the same constructors from ObservableRecipient are emitted, with the accessibility adapted to that of the annotated type. Otherwise, they are skipped, so the type being annotated has the responsibility of properly initializing the Messenger property. Additionally, if the annotated type inherits from ObservableValidator, the SetProperty<T>(ref T, T, bool, string?) overloads will be skipped as well, as they would conflict with the SetProperty<T>(ref T, T, bool, string) methods.
- ObservableValidator
A base class for objects implementing the INotifyDataErrorInfo interface. This class also inherits from ObservableObject, so it can be used for observable items too.