-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComputedBindableBase.cs
38 lines (34 loc) · 1.27 KB
/
ComputedBindableBase.cs
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
using System.Collections.Generic;
using System.Reflection;
namespace ViewModel
{
public abstract class ComputedBindableBase : BindableBase
{
public ComputedBindableBase()
{
var properties = new Dictionary<string, HashSet<string>>();
foreach (var property in this.GetType().GetTypeInfo().DeclaredProperties)
{
var computedAttribute = property.GetCustomAttribute<PropertySourceAttribute>();
if (computedAttribute == null) {
continue;
}
foreach (var sourceName in computedAttribute.Sources)
{
if (!properties.ContainsKey(sourceName)) {
properties[sourceName] = new HashSet<string>();
}
properties[sourceName].Add(property.Name);
}
}
PropertyChanged += (sender, e) => {
if (properties.ContainsKey(e.PropertyName)) {
foreach (var computedPropertyName in properties[e.PropertyName])
{
RaisePropertyChanged(computedPropertyName);
}
}
};
}
}
}