Skip to content

Instantly share code, notes, and snippets.

@ngbrown
Forked from jpoehls/NotificationExtensions.cs
Last active December 19, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngbrown/6018001 to your computer and use it in GitHub Desktop.
Save ngbrown/6018001 to your computer and use it in GitHub Desktop.
public abstract class BindableBase : INotifyPropertyChanged
{
/// <summary>
/// Multicast event for property change notifications.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
[NotifyPropertyChangedInvocator]
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class NotificationExtensions
{
/// <summary>
/// Subscribe to PropertyChange notifications for the given property.
/// </summary>
/// <param name="notifier">
/// Implementer of <see cref="INotifyPropertyChanged"/> that the events are going to be coming from.
/// </param>
/// <param name="propertyExpression">
/// Lambda expression pointing to the property that you want to receive change notifications for.
/// </param>
/// <param name="handler">
/// Event handler of the PropertyChange notifications.
/// </param>
public static void SubscribeToChanges<T>(
this T notifier,
Expression<Func<T, object>> propertyExpression,
PropertyChangedEventHandler handler)
where T : INotifyPropertyChanged
{
MemberExpression memberExpression;
var unaryExpression = propertyExpression.Body as UnaryExpression;
if (unaryExpression != null)
{
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else
{
memberExpression = (MemberExpression)propertyExpression.Body;
}
var propertyInfo = (PropertyInfo)memberExpression.Member;
string propertyName = propertyInfo.Name;
notifier.PropertyChanged += (s, e) =>
{
if (e.PropertyName.Equals(propertyName) || string.IsNullOrEmpty(e.PropertyName))
{
handler(s, e);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment