Repository navigation
Expand file tree
/
Copy pathNotifyPropertyChangedEx.cs
More file actions
162 lines (139 loc) · 6.28 KB
/
Copy pathNotifyPropertyChangedEx.cs
File metadata and controls
162 lines (139 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reactive.Linq;
using System.Reflection;
using DynamicData.Annotations;
namespace DynamicData.Binding
{
/// <summary>
/// Property changes notification
/// </summary>
public static class NotifyPropertyChangedEx
{
/// <summary>
/// Observes property changes for the specified property, starting with the current value
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <param name="propertyAccessor">The property accessor.</param>
/// <param name="notifyOnInitialValue">if set to <c>true</c> [notify on initial value].</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">propertyAccessor</exception>
public static IObservable<PropertyValue<TObject, TValue>> WhenChanged<TObject, TValue>(
[NotNull] this TObject source,
Expression<Func<TObject, TValue>> propertyAccessor, bool notifyOnInitialValue=true)
where TObject : INotifyPropertyChanged
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (propertyAccessor == null) throw new ArgumentNullException(nameof(propertyAccessor));
var member = propertyAccessor.GetProperty();
var accessor = propertyAccessor.Compile();
Func<PropertyValue<TObject, TValue>> factory =
() => new PropertyValue<TObject, TValue>(source, accessor(source));
var propertyChanged = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>
(
handler => source.PropertyChanged += handler,
handler => source.PropertyChanged -= handler
)
.Where(args => args.EventArgs.PropertyName == member.Name)
.Select(x => factory());
return !notifyOnInitialValue ? propertyChanged : propertyChanged.StartWith(factory());
}
/// <summary>
/// Observes property changes for the specified property, starting with the current value
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <param name="propertyAccessor">The property accessor.</param>
/// <param name="notifyOnInitialValue">if set to <c>true</c> [notify on initial value].</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public static IObservable<TValue> WhenValueChanged<TObject, TValue>([NotNull] this TObject source,Expression<Func<TObject, TValue>> propertyAccessor, bool notifyOnInitialValue = true)
where TObject : INotifyPropertyChanged
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (propertyAccessor == null) throw new ArgumentNullException(nameof(propertyAccessor));
var member = propertyAccessor.GetProperty();
var accessor = propertyAccessor.Compile();
Func<PropertyValue<TObject, TValue>> factory =
() => new PropertyValue<TObject, TValue>(source, accessor(source));
var propertyChanged = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>
(
handler => source.PropertyChanged += handler,
handler => source.PropertyChanged -= handler
)
.Where(args => args.EventArgs.PropertyName == member.Name)
.Select(x => accessor(source));
return !notifyOnInitialValue ? propertyChanged : propertyChanged.StartWith(accessor(source));
}
/// <summary>
/// Selects the value from the property changed notification
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
public static IObservable<TValue> Value<TObject, TValue>([NotNull] this IObservable<PropertyValue<TObject, TValue>> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.Select(prop => prop.Value);
}
/// <summary>
/// Observes property changed for the specified object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IObservable<string> ObservePropertyChanges<T>(this T source)
where T : INotifyPropertyChanged
{
return Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>
(
handler => source.PropertyChanged += handler,
handler => source.PropertyChanged -= handler
)
.Select(x => x.EventArgs.PropertyName);
}
private static PropertyInfo GetProperty<TObject, TProperty>(this Expression<Func<TObject, TProperty>> expression)
{
var property = GetMember(expression) as PropertyInfo;
if (property == null)
throw new ArgumentException("Not a property expression");
return property;
}
private static MemberInfo GetMember<TObject, TProperty>(this Expression<Func<TObject, TProperty>> expression)
{
if (expression == null)
throw new ArgumentException("Not a property expression");
return GetMemberInfo(expression);
}
private static MemberInfo GetMemberInfo(LambdaExpression lambda)
{
if (lambda == null)
throw new ArgumentException("Not a property expression");
MemberExpression memberExpression = null;
if (lambda.Body.NodeType == ExpressionType.Convert)
{
memberExpression = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
}
else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
{
memberExpression = lambda.Body as MemberExpression;
}
else if (lambda.Body.NodeType == ExpressionType.Call)
{
return ((MethodCallExpression)lambda.Body).Method;
}
if (memberExpression == null)
{
throw new ArgumentException("Not a member access");
}
return memberExpression.Member;
}
}
}