Created
April 30, 2010 16:40
-
-
Save jpoehls/385450 to your computer and use it in GitHub Desktop.
INotifyPropertyChanged Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace Samples | |
{ | |
public static class NotificationExtensions | |
{ | |
/// <summary> | |
/// Subscribe to PropertyChange notifications for the | |
/// given property. | |
/// </summary> | |
/// <param name="propertyExpression">Lambda expression pointing to the property that you want to receive change notifications for.</param> | |
public static void SubscribeToChanges<T>(this T notifier, Expression<Func<object>> propertyExpression, | |
PropertyChangedEventHandler handler) | |
where T : INotifyPropertyChanged | |
{ | |
notifier.PropertyChanged += | |
(s, e) => | |
{ | |
var lambda = propertyExpression as LambdaExpression; | |
MemberExpression memberExpression; | |
if (lambda.Body is UnaryExpression) | |
{ | |
var unaryExpression = lambda.Body as UnaryExpression; | |
memberExpression = unaryExpression.Operand as MemberExpression; | |
} | |
else | |
{ | |
memberExpression = lambda.Body as MemberExpression; | |
} | |
var propertyInfo = memberExpression.Member as PropertyInfo; | |
if (e.PropertyName.Equals(propertyInfo.Name)) | |
{ | |
handler(notifier, e); | |
} | |
}; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace Samples | |
{ | |
public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged | |
{ | |
#region INotifyPropertyChanged Members | |
public event PropertyChangedEventHandler PropertyChanged; | |
#endregion | |
/// <summary> | |
/// Raises the PropertyChanged event for the given property. | |
/// </summary> | |
/// <param name="propertyExpression">Lambda expression pointing to the property that the change notification should be raised for.</param> | |
/// <example> | |
/// Notify( () => PropertyThatChanged ); | |
/// </example> | |
public void Notify(Expression<Func<object>> propertyExpression) | |
{ | |
if (PropertyChanged == null) | |
return; | |
var lambda = propertyExpression as LambdaExpression; | |
MemberExpression memberExpression; | |
if (lambda.Body is UnaryExpression) | |
{ | |
var unaryExpression = lambda.Body as UnaryExpression; | |
memberExpression = unaryExpression.Operand as MemberExpression; | |
} | |
else | |
{ | |
memberExpression = lambda.Body as MemberExpression; | |
} | |
if (memberExpression == null) | |
{ | |
throw new ArgumentException("Expression must be a UnaryExpression or MemberExpression.", | |
"propertyExpression"); | |
} | |
var propertyInfo = memberExpression.Member as PropertyInfo; | |
if (propertyInfo == null) | |
{ | |
throw new ArgumentException("Expression must point to a property.", "propertyExpression"); | |
} | |
PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo.Name)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment