CommunityToolkit / CommunityToolkit/dotnet

`RaisePropertyChanged` method in `ObservableObject`

Open
#1,086 4 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

Trying to replace an internal, legacy implementation of `ObservableCollection` with the one in CommunityToolkit it was noticed there is no way to simply raise the `PropertyChanged` event without invoking OnPropertyChanged and any overrides.

This functionality is relied upon in some cases and it's time consuming to work around. Since it seems harmless adding upstream here to `ObservableObject` I created this ticket.

### API breakdown

```csharp
class ObservableObject
{
protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(new PropertyChangedEventArgs(propertyName));
}

protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(e);
}
}
```

### Usage example

The cases where this is most useful are those where you are doing something within an OnPropertyChanged override and want to raise some event manually (you just calculated something manually). If you use OnPropertyChanged again it's going to crash with a stack overflow as it keeps calling itself. Of course you can guard against this but it seems easy enough just to support the ability to raise the event directly. Tooling should generally let you do what you want to do while guiding towards a proper flow. Not restrict you completely.

### Breaking change?

No

### Alternatives

None in this context. Would just have to force all code to work with OnPropertyChanged only.

### Additional context

_No response_

### Help us help you

No, just wanted to propose this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.