forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveBinding.cs
More file actions
217 lines (197 loc) · 7.74 KB
/
ReactiveBinding.cs
File metadata and controls
217 lines (197 loc) · 7.74 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReactiveUI
{
/// <summary>
/// This interface represents the result of a Bind/OneWayBind and gives
/// information about the binding. When this object is disposed, it will
/// destroy the binding it is describing (i.e. most of the time you won't
/// actually care about this object, just that it is disposable)
/// </summary>
public interface IReactiveBinding<TView, TViewModel, TValue> : IDisposable
where TViewModel : class
where TView : IViewFor
{
/// <summary>
/// The instance of the view model this binding is applied to.</param>
/// </summary>
/// <value>
/// The view model.
/// </value>
TViewModel ViewModel { get; }
/// <summary>
/// An array representing the property names on the viewmodel bound to the view.
/// This can be a child property, for example x.Foo.Bar.Baz</c> in which case
/// the path will contain "Foo","Bar","Baz".
/// </summary>
/// <value>
/// The property names of the path.
/// </value>
string[] ViewModelPath { get; }
/// <summary>
/// An enumerable representing the properties on the viewmodel bound to the view.
/// This can contain child properties, for example x.Foo.Bar.Baz</c> in which case
/// it will contain the Foo, Bar and Baz properties.
/// </summary>
/// <value>
/// The properties of the path.
/// </value>
IObservedChange<object, object>[] ViewModelPathProperties { get; }
/// <summary>
/// The instance of the view this binding is applied to.
/// </summary>
/// <value>
/// The view.
/// </value>
TView View { get; }
/// <summary>
/// An array representing the property names on the view bound to the viewmodel.
/// This can be a child property, for example x.Foo.Bar.Baz</c> in which case
/// the path will contain "Foo","Bar","Baz".
/// </summary>
/// <value>
/// The property names of the path.
/// </value>
string[] ViewPath { get; }
/// <summary>
/// An enumerable representing the properties on the view bound to the viewmodel.
/// This can contain child properties, for example x.Foo.Bar.Baz</c> in which case
/// it will contain the Foo, Bar and Baz properties.
/// </summary>
/// <value>
/// The properties of the path.
/// </value>
IObservedChange<object, object>[] ViewPathProperties { get; }
/// <summary>
/// An observable representing changed values for the binding.
/// </summary>
/// <value>
/// The changed.
/// </value>
IObservable<TValue> Changed { get; }
/// <summary>
/// Gets the direction of the binding.
/// </summary>
/// <value>
/// The direction.
/// </value>
BindingDirection Direction { get; }
}
internal class ReactiveBinding<TView, TViewModel, TValue> : IReactiveBinding<TView, TViewModel, TValue>
where TViewModel : class
where TView : IViewFor
{
private IDisposable bindingDisposable;
/// <summary>
/// Initializes a new instance of the <see cref="AppliedBindingInfo{TViewModel}" /> class.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="viewModel">The view model.</param>
/// <param name="viewPath">The view path.</param>
/// <param name="viewModelPath">The view model path.</param>
/// <param name="direction">The direction.</param>
/// <param name="bindingDisposable">The binding disposable.</param>
public ReactiveBinding(TView view, TViewModel viewModel, string[] viewPath, string[] viewModelPath,
IObservable<TValue> changed, BindingDirection direction, IDisposable bindingDisposable)
{
this.View = view;
this.ViewModel = viewModel;
this.ViewPath = viewPath;
this.ViewModelPath = viewModelPath;
this.Direction = direction;
this.Changed = changed;
this.bindingDisposable = bindingDisposable;
}
/// <summary>
/// The instance of the view model this binding is applied to.</param>
/// </summary>
/// <value>
/// The view model.
/// </value>
public TViewModel ViewModel { get; private set; }
/// <summary>
/// An array representing the property names on the viewmodel bound to the view.
/// This can be a child property, for example x.Foo.Bar.Baz</c> in which case
/// the path will contain "Foo","Bar","Baz".
/// </summary>
/// <value>
/// The property names of the path.
/// </value>
public string[] ViewModelPath { get; private set; }
/// <summary>
/// An enumerable representing the properties on the viewmodel bound to the view.
/// This can contain child properties, for example x.Foo.Bar.Baz</c> in which case
/// it will contain the Foo, Bar and Baz properties.
/// </summary>
/// <value>
/// The properties of the path.
/// </value>
public IObservedChange<object, object>[] ViewModelPathProperties
{
get {
IObservedChange<object, object>[] fetchedValues;
Reflection.TryGetAllValuesForPropertyChain(out fetchedValues, ViewModel, ViewModelPath);
return fetchedValues;
}
}
/// <summary>
/// The instance of the view this binding is applied to.
/// </summary>
/// <value>
/// The view.
/// </value>
public TView View { get; private set; }
/// <summary>
/// An array representing the property names on the view bound to the viewmodel.
/// This can be a child property, for example x.Foo.Bar.Baz</c> in which case
/// the path will contain "Foo","Bar","Baz".
/// </summary>
/// <value>
/// The property names of the path.
/// </value>
public string[] ViewPath { get; private set;}
/// <summary>
/// An enumerable representing the properties on the view bound to the viewmodel.
/// This can contain child properties, for example x.Foo.Bar.Baz</c> in which case
/// it will contain the Foo, Bar and Baz properties.
/// </summary>
/// <value>
/// The properties of the path.
/// </value>
public IObservedChange<object, object>[] ViewPathProperties
{
get {
IObservedChange<object, object>[] fetchedValues;
Reflection.TryGetAllValuesForPropertyChain(out fetchedValues, View, ViewPath);
return fetchedValues;
}
}
/// <summary>
/// An observable representing changed values for the binding.
/// </summary>
/// <value>
/// The changed.
/// </value>
public IObservable<TValue> Changed { get; private set; }
/// <summary>
/// Gets the direction of the binding.
/// </summary>
/// <value>
/// The direction.
/// </value>
public BindingDirection Direction { get; private set; }
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
if (bindingDisposable != null) {
bindingDisposable.Dispose();
bindingDisposable = null;
}
}
}
}