diff --git a/docs/mvvm/generators/ObservableProperty.md b/docs/mvvm/generators/ObservableProperty.md index 2125ca852..fd139c219 100644 --- a/docs/mvvm/generators/ObservableProperty.md +++ b/docs/mvvm/generators/ObservableProperty.md @@ -224,7 +224,7 @@ public string? Name if (SetProperty(ref name, value)) { - Broadcast(oldValue, value); + Broadcast(oldValue, value, "Name"); } } } @@ -232,6 +232,30 @@ public string? Name That generated `Broadcast` call will then send a new [`PropertyChangedMessage`](/dotnet/api/microsoft.toolkit.mvvm.Messaging.Messages.PropertyChangedMessage-1) using the `IMessenger` instance in use in the current viewmodel, to all registered subscribers. +Example implementation using the default `WeakReferenceMessenger` implementation in `ObservableRecipient`: + +```csharp + +//This class sends a PropertyChanged message when the "Name" property changes +public partial class SenderViewModel : ObservableRecipient +{ + [ObservableObject] + [NotifyPropertyChangedRecipients] + private string? _name; +} + +//This class received a PropertyChanged message +public class ReceiverViewModel : ObservableRecipient, IRecipient> +{ + public ReceiverViewModel() => IsActive = true; + + public void Receive(PropertyChangedMessage message) => Trace.WriteLine($"Name Changed to: {message.NewValue}"); +} + + +``` + + ## Adding custom attributes In some cases, it might be useful to also have some custom attributes over the generated properties. To achieve that, you can simply use the `[property: ]` target in attribute lists over annotated fields, and the MVVM Toolkit will automatically forward those attributes to the generated properties.