Home

OnPropertyChanged

OnPropertyChanged is a convention used in .NET to raise PropertyChanged events to notify data-binding clients that a property value has changed. It is part of the INotifyPropertyChanged interface in System.ComponentModel and is essential in MVVM and similar patterns in WPF, UWP, Xamarin.Forms, MAUI, and Blazor.

The interface defines an event: event PropertyChangedEventHandler PropertyChanged; A common helper method named OnPropertyChanged triggers the

Modern code often uses CallerMemberName to avoid specifying names: protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this,

Example of a typical property with change notification:

private string _name;

public string Name

{

get => _name;

set

{

if (_name != value)

{

_name = value;

OnPropertyChanged();

}

}

}

Notes and best practices include that OnPropertyChanged should be called after updating the backing field, and

event:
OnPropertyChanged(string
propertyName)
{
PropertyChanged?.Invoke(this,
new
PropertyChangedEventArgs(propertyName));
}
new
PropertyChangedEventArgs(propertyName));
}
with
using
System.Runtime.CompilerServices;.
Then
usage:
OnPropertyChanged()
inside
a
property's
setter
after
the
value
changes.
only
when
the
value
actually
changes.
Some
frameworks
provide
helper
methods,
such
as
SetProperty,
to
reduce
boilerplate.
If
updates
occur
on
background
threads,
changes
bound
to
the
UI
should
be
marshaled
to
the
UI
thread,
as
the
PropertyChanged
event
itself
does
not
enforce
threading.
The
pattern
remains
widely
used
with
INotifyPropertyChanged
across
leading
.NET
UI
frameworks.