Created
May 20, 2014 04:44
-
-
Save patsissons/ea2c2ba3dcc172559dde to your computer and use it in GitHub Desktop.
INotifyPropertyChanged to Observable (portable FromPropertyChangedPattern)
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
namespace Test | |
{ | |
public static class INotifyPropertyChangedExtensions | |
{ | |
public static IObservable<EventPattern<PropertyChangedEventArgs>> FromPropertyChangedPattern(this INotifyPropertyChanged source) | |
{ | |
return Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>( | |
x => source.PropertyChanged += x, | |
x => source.PropertyChanged -= x); | |
} | |
public static IObservable<EventPattern<PropertyChangedEventArgs>> WhenAnyPropertyChanges<T>(this T source, params Expression<Func<T, object>>[] properties) | |
where T : INotifyPropertyChanged | |
{ | |
return WhenAnyPropertyChanges(source, FromPropertyChangedPattern(source), properties); | |
} | |
public static IObservable<EventPattern<PropertyChangedEventArgs>> WhenAnyPropertyChanges<T>(this T source, IObservable<EventPattern<PropertyChangedEventArgs>> observable, params Expression<Func<T, object>>[] properties) | |
where T : INotifyPropertyChanged | |
{ | |
return WhenAnyPropertyChanges<T>(observable, properties); | |
} | |
public static IObservable<EventPattern<PropertyChangedEventArgs>> WhenAnyPropertyChanges<T>(this IObservable<EventPattern<PropertyChangedEventArgs>> source, params Expression<Func<T, object>>[] properties) | |
{ | |
return WhenAnyPropertyEventOccurs(source, x => x.EventArgs.PropertyName, properties); | |
} | |
public static IObservable<TObservable> WhenAnyPropertyEventOccurs<T, TObservable>(this IObservable<TObservable> source, Func<TObservable, string> propertyNameSelector, params Expression<Func<T, object>>[] properties) | |
{ | |
var propertyNames = properties | |
.Select(x => x.Body) | |
.OfType<MemberExpression>() | |
.Select(x => x.Member.Name); | |
return source | |
.Where(x => propertyNames.Any(y => EqualityComparer<string>.Default.Equals(y, propertyNameSelector(x)))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @patsissons. Any chance we could see usage of this somewhere? PArticularly
WhenAnyPropertyChanges
.