Skip to content

RaisePropertyChanged method in ObservableObject #1086

@robert-abeo

Description

@robert-abeo

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions