/
AutoRefreshForPropertyChanges.cs
47 lines (40 loc) · 1.48 KB
/
AutoRefreshForPropertyChanges.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
39
40
41
42
43
44
45
46
47
using System;
using DynamicData.Binding;
using DynamicData.Snippets.Infrastructure;
namespace DynamicData.Snippets.AutoRefresh
{
public class AutoRefreshForPropertyChanges
{
public IObservable<int> DistinctCount { get; }
public AutoRefreshForPropertyChanges(IObservableCache<MutableThing, int> dataSource)
{
/*
* The observable cache has no concept of mutable properties. It only knows about adds, updates and removes.
* However it does have the concept of a refresh, which is a manual way to tell downstream operators like
* distinct, sort, filter and grouping to re-evaluate
*
* To force a refresh, you need to manually trigger when to do so. In this case property changes are monitored
* and the data source sends a refresh signal to all downstream operators.
*/
DistinctCount = dataSource.Connect()
.AutoRefresh(t=>t.Value) //Omit param to refresh for any property
.DistinctValues(m => m.Value)
.Count();
}
}
public class MutableThing : AbstractNotifyPropertyChanged
{
private string _value;
public MutableThing(int id, string value)
{
Id = id;
Value = value;
}
public int Id { get; }
public string Value
{
get => _value;
set => SetAndRaise(ref _value, value);
}
}
}