Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion docs/mvvm/generators/ObservableProperty.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,38 @@ public string? Name

if (SetProperty(ref name, value))
{
Broadcast(oldValue, value);
Broadcast(oldValue, value, "Name");
}
}
}
```

That generated `Broadcast` call will then send a new [`PropertyChangedMessage<T>`](/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<PropertyChangedMessage<string>>
{
public ReceiverViewModel() => IsActive = true;

public void Receive(PropertyChangedMessage<string> 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.
Expand Down