-
Notifications
You must be signed in to change notification settings - Fork 14
/
MultiBinding.cs
310 lines (291 loc) · 13 KB
/
MultiBinding.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using System.Xaml;
namespace QuickConverter
{
/// <summary>
/// This type can be substituted for System.Windows.Data.MultiBinding. Multiple bindings and a one way converter can be specified inline.
/// </summary>
public class MultiBinding : MarkupExtension
{
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P0.</summary>
public BindingBase P0 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P1.</summary>
public BindingBase P1 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P2.</summary>
public BindingBase P2 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P3.</summary>
public BindingBase P3 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P4.</summary>
public BindingBase P4 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P5.</summary>
public BindingBase P5 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P6.</summary>
public BindingBase P6 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P7.</summary>
public BindingBase P7 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P8.</summary>
public BindingBase P8 { get; set; }
/// <summary>Creates a bound parameter. This can be accessed inside the converter as $P9.</summary>
public BindingBase P9 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V0.</summary>
public object V0 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V1.</summary>
public object V1 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V2.</summary>
public object V2 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V3.</summary>
public object V3 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V4.</summary>
public object V4 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V5.</summary>
public object V5 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V6.</summary>
public object V6 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V7.</summary>
public object V7 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V8.</summary>
public object V8 { get; set; }
/// <summary>Creates a constant parameter. This can be accessed inside the converter as $V9.</summary>
public object V9 { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P0 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P0Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P1 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P1Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P2 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P2Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P3 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P3Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P4 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P4Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P5 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P5Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P6 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P6Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P7 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P7Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P8 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P8Type { get; set; }
/// <summary>The converter will return DependencyObject.Unset during conversion if P9 is not of this type. Both QuickConverter syntax (as a string) and Type objects are valid.</summary>
public object P9Type { get; set; }
/// <summary>
/// The expression to use for converting data from the source.
/// </summary>
public string Converter { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P0.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack0 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P1.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack1 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P2.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack2 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P3.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack3 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P4.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack4 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P5.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack5 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P6.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack6 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P7.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack7 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P8.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack8 { get; set; }
/// <summary>
/// The expression to use for converting data from the target back to the source for P9.
/// The target value is accessible as $value.
/// The bound parameters $P0-$P9 cannot be accessed when converting back.
/// </summary>
public string ConvertBack9 { get; set; }
/// <summary>
/// The conventional parameter passed into converters.
/// This is accessible as $par inside converter expressions.
/// </summary>
public object ConverterParameter { get; set; }
/// <summary>
/// This chooses whether to pass the converter parameter or V0-V9 as an array into the external converter.
/// </summary>
public ConverterParameterMode ExternalConverterParameterMode { get; set; }
private bool _modeSet = false;
private BindingMode _mode;
/// <summary>
/// Sets the binding mode.
/// </summary>
public BindingMode Mode
{
get { return _mode; }
set
{
_mode = value;
_modeSet = true;
}
}
/// <summary>
/// The value that is used in the target when the value of the source is null.
/// </summary>
public object TargetNullValue { get; set; }
/// <summary>
/// The value to use when the binding is unable to return a value.
/// </summary>
public object FallbackValue { get; set; }
/// <summary>
/// Sets an override converter.
/// </summary>
public IMultiValueConverter ExternalConverter { get; set; }
/// <summary>
/// Sets the update source trigger.
/// </summary>
public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
/// <summary>
/// This specifies the context to use for dynamic call sites.
/// </summary>
public Type DynamicContext { get; set; }
public MultiBinding()
{
}
public MultiBinding(string converter)
{
Converter = converter;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
try
{
bool getExpression;
if (serviceProvider == null)
getExpression = false;
else
{
var targetProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (targetProvider != null && (targetProvider.TargetObject is Setter))
getExpression = false;
else if (targetProvider == null || !(targetProvider.TargetProperty is PropertyInfo))
getExpression = true;
else
{
Type propType = (targetProvider.TargetProperty as PropertyInfo).PropertyType;
if (propType == typeof(MultiBinding))
return this;
getExpression = !propType.IsAssignableFrom(typeof(System.Windows.Data.MultiBinding));
}
}
var holder = new System.Windows.Data.MultiBinding() { Mode = Mode, UpdateSourceTrigger = UpdateSourceTrigger };
if (FallbackValue != null)
holder.FallbackValue = FallbackValue;
if (TargetNullValue != null)
holder.TargetNullValue = TargetNullValue;
if (ExternalConverter == null)
{
holder.ConverterParameter = ConverterParameter;
string[] parameterOrder;
holder.Converter = new QuickMultiConverter()
{
Converter = Converter,
DynamicContext = DynamicContext,
P0Type = P0Type,
P1Type = P1Type,
P2Type = P2Type,
P3Type = P3Type,
P4Type = P4Type,
P5Type = P5Type,
P6Type = P6Type,
P7Type = P7Type,
P8Type = P8Type,
P9Type = P9Type,
V0 = V0,
V1 = V1,
V2 = V2,
V3 = V3,
V4 = V4,
V5 = V5,
V6 = V6,
V7 = V7,
V8 = V8,
V9 = V9,
ConvertBack0 = ConvertBack0,
ConvertBack1 = ConvertBack1,
ConvertBack2 = ConvertBack2,
ConvertBack3 = ConvertBack3,
ConvertBack4 = ConvertBack4,
ConvertBack5 = ConvertBack5,
ConvertBack6 = ConvertBack6,
ConvertBack7 = ConvertBack7,
ConvertBack8 = ConvertBack8,
ConvertBack9 = ConvertBack9
}.Get(out parameterOrder);
foreach (string name in parameterOrder)
holder.Bindings.Add(typeof(MultiBinding).GetProperty(name).GetValue(this, null) as BindingBase);
if (!_modeSet)
{
bool exists = false;
for (int i = 0; i <= 9; ++i)
exists |= !String.IsNullOrWhiteSpace(typeof(MultiBinding).GetProperty("ConvertBack" + i).GetValue(this, null) as string);
holder.Mode = exists ? BindingMode.TwoWay : BindingMode.OneWay;
}
}
else
{
if (ExternalConverterParameterMode == ConverterParameterMode.Parameter)
holder.ConverterParameter = ConverterParameter;
else
holder.ConverterParameter = new object[] { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9 };
holder.Converter = ExternalConverter;
for (int i = 0; i <= 9; ++i)
{
var binding = typeof(MultiBinding).GetProperty("P" + i).GetValue(this, null) as BindingBase;
if (binding != null)
holder.Bindings.Add(binding);
}
}
return getExpression ? holder.ProvideValue(serviceProvider) : holder;
}
catch (Exception e)
{
EquationTokenizer.ThrowQuickConverterEvent(new MarkupExtensionExceptionEventArgs(Converter, this, e));
throw;
}
}
}
}