-
Notifications
You must be signed in to change notification settings - Fork 777
/
Copy pathBind.cs
285 lines (257 loc) · 9.77 KB
/
Bind.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
#if XFORMS
namespace Caliburn.Micro.Xamarin.Forms
#elif MAUI
namespace Caliburn.Micro.Maui
#else
namespace Caliburn.Micro
#endif
{
using System;
#if WINDOWS_UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
#elif WinUI3
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
#elif XFORMS
using global::Xamarin.Forms;
using UIElement = global::Xamarin.Forms.Element;
using FrameworkElement = global::Xamarin.Forms.VisualElement;
using DependencyProperty = global::Xamarin.Forms.BindableProperty;
using DependencyObject = global::Xamarin.Forms.BindableObject;
#elif AVALONIA
using Avalonia;
using Avalonia.Data;
using DependencyObject = Avalonia.AvaloniaObject;
using DependencyPropertyChangedEventArgs = Avalonia.AvaloniaPropertyChangedEventArgs;
using FrameworkElement = Avalonia.Controls.Control;
using DependencyProperty = Avalonia.AvaloniaProperty;
#elif MAUI
using global::Microsoft.Maui;
using UIElement = global::Microsoft.Maui.Controls.Element;
using FrameworkElement = global::Microsoft.Maui.Controls.VisualElement;
using DependencyProperty = global::Microsoft.Maui.Controls.BindableProperty;
using DependencyObject = global::Microsoft.Maui.Controls.BindableObject;
#else
using System.Windows;
using System.Windows.Data;
#endif
/// <summary>
/// Hosts dependency properties for binding.
/// </summary>
public static class Bind
{
/// <summary>
/// Allows binding on an existing view. Use this on root UserControls, Pages and Windows; not in a DataTemplate.
/// </summary>
public static DependencyProperty ModelProperty =
#if AVALONIA
AvaloniaProperty.RegisterAttached<AvaloniaObject, object>("Model", typeof(Bind));
#else
DependencyPropertyHelper.RegisterAttached(
"Model",
typeof(object),
typeof(Bind),
null,
ModelChanged);
#endif
/// <summary>
/// Allows binding on an existing view without setting the data context. Use this from within a DataTemplate.
/// </summary>
public static DependencyProperty ModelWithoutContextProperty =
#if AVALONIA
AvaloniaProperty.RegisterAttached<AvaloniaObject, object>("Handler", typeof(Bind));
#else
DependencyPropertyHelper.RegisterAttached(
"ModelWithoutContext",
typeof(object),
typeof(Bind),
null,
ModelWithoutContextChanged);
#endif
internal static readonly DependencyProperty NoContextProperty =
#if AVALONIA
AvaloniaProperty.RegisterAttached<AvaloniaObject, bool>("Handler", typeof(Bind));
#else
DependencyPropertyHelper.RegisterAttached(
"NoContext",
typeof(bool),
typeof(Bind),
false);
#endif
#if AVALONIA
static Bind()
{
ModelProperty.Changed.Subscribe(args => ModelChanged(args.Sender, args));
ModelWithoutContextProperty.Changed.Subscribe(args => ModelWithoutContextChanged(args.Sender, args));
DataContextProperty.Changed.Subscribe(args => DataContextChanged(args.Sender, args));
AtDesignTimeProperty.Changed.Subscribe(args => AtDesignTimeChanged(args.Sender, args));
}
#endif
/// <summary>
/// Gets the model to bind to.
/// </summary>
/// <param name = "dependencyObject">The dependency object to bind to.</param>
/// <returns>The model.</returns>
public static object GetModelWithoutContext(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(ModelWithoutContextProperty);
}
/// <summary>
/// Sets the model to bind to.
/// </summary>
/// <param name = "dependencyObject">The dependency object to bind to.</param>
/// <param name = "value">The model.</param>
public static void SetModelWithoutContext(DependencyObject dependencyObject, object value)
{
dependencyObject.SetValue(ModelWithoutContextProperty, value);
}
/// <summary>
/// Gets the model to bind to.
/// </summary>
/// <param name = "dependencyObject">The dependency object to bind to.</param>
/// <returns>The model.</returns>
public static object GetModel(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(ModelProperty);
}
/// <summary>
/// Sets the model to bind to.
/// </summary>
/// <param name = "dependencyObject">The dependency object to bind to.</param>
/// <param name = "value">The model.</param>
public static void SetModel(DependencyObject dependencyObject, object value)
{
dependencyObject.SetValue(ModelProperty, value);
}
static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (View.InDesignMode || object.ReferenceEquals(e.NewValue, e.OldValue))
{
return;
}
var fe = d as FrameworkElement;
if (fe == null)
{
return;
}
View.ExecuteOnLoad(fe, delegate
{
var target = e.NewValue;
d.SetValue(View.IsScopeRootProperty, true);
#if XFORMS || MAUI
var context = fe.Id.ToString("N");
#else
var context = string.IsNullOrEmpty(fe.Name)
? fe.GetHashCode().ToString()
: fe.Name;
#endif
ViewModelBinder.Bind(target, d, context);
});
}
static void ModelWithoutContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (View.InDesignMode || object.ReferenceEquals(e.NewValue, e.OldValue))
{
return;
}
var fe = d as FrameworkElement;
if (fe == null)
{
return;
}
View.ExecuteOnLoad(fe, delegate
{
var target = e.NewValue;
d.SetValue(View.IsScopeRootProperty, true);
#if XFORMS || MAUI
var context = fe.Id.ToString("N");
#else
var context = string.IsNullOrEmpty(fe.Name)
? fe.GetHashCode().ToString()
: fe.Name;
#endif
d.SetValue(NoContextProperty, true);
ViewModelBinder.Bind(target, d, context);
});
}
/// <summary>
/// Allows application of conventions at design-time.
/// </summary>
public static DependencyProperty AtDesignTimeProperty =
#if AVALONIA
AvaloniaProperty.RegisterAttached<AvaloniaObject, bool>("AtDesignTime", typeof(Bind));
#else
DependencyPropertyHelper.RegisterAttached(
"AtDesignTime",
typeof(bool),
typeof(Bind),
false,
AtDesignTimeChanged);
#endif
/// <summary>
/// Gets whether or not conventions are being applied at design-time.
/// </summary>
/// <param name="dependencyObject">The ui to apply conventions to.</param>
/// <returns>Whether or not conventions are applied.</returns>
#if (NET || CAL_NETCORE) && (!AVALONIA && !WINDOWS_UWP && !MAUI) && !WinUI3 // not sure this is right
[AttachedPropertyBrowsableForTypeAttribute(typeof(DependencyObject))]
#endif
public static bool GetAtDesignTime(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(AtDesignTimeProperty);
}
/// <summary>
/// Sets whether or not do bind conventions at design-time.
/// </summary>
/// <param name="dependencyObject">The ui to apply conventions to.</param>
/// <param name="value">Whether or not to apply conventions.</param>
public static void SetAtDesignTime(DependencyObject dependencyObject, bool value)
{
dependencyObject.SetValue(AtDesignTimeProperty, value);
}
static void AtDesignTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!View.InDesignMode)
return;
var atDesignTime = (bool)e.NewValue;
if (!atDesignTime)
return;
#if XFORMS
d.SetBinding(DataContextProperty, string.Empty);
#elif AVALONIA
d.Bind(DataContextProperty, new Binding());
#elif MAUI
d.SetBinding(DataContextProperty, null);
#else
BindingOperations.SetBinding(d, DataContextProperty, new Binding());
#endif
}
static readonly DependencyProperty DataContextProperty =
#if AVALONIA
AvaloniaProperty.RegisterAttached<AvaloniaObject, object>("DataContext", typeof(Bind));
#else
DependencyPropertyHelper.RegisterAttached(
"DataContext",
typeof(object),
typeof(Bind),
null, DataContextChanged);
#endif
static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!View.InDesignMode)
return;
var enable = d.GetValue(AtDesignTimeProperty);
if (enable == null || !((bool)enable) || e.NewValue == null)
return;
var fe = d as FrameworkElement;
if (fe == null)
return;
#if XFORMS || MAUI
ViewModelBinder.Bind(e.NewValue, d, fe.Id.ToString("N"));
#else
ViewModelBinder.Bind(e.NewValue, d, string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name);
#endif
}
}
}