summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/Helpers/ViewHelper.cs
blob: 9c17374f8d024b26e13e6f4a40427066ac1fc523 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using CurrencyTextBoxControl;
using Gestor.Application.Componentes;
using Gestor.Model.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using Xceed.Wpf.Toolkit;
using Xceed.Wpf.Toolkit.Primitives;

namespace Gestor.Application.Helpers
{
	public static class ViewHelper
	{
		internal static void BindData<T>(this CheckComboBox comboBox, bool withSelect = true, bool enumDefault = true, string defaultValue = "", bool orderByLabel = false)
		{
			ViewHelper.DefaultBinding<T>(comboBox, withSelect, enumDefault, defaultValue, string.Empty, orderByLabel);
		}

		public static void ClearInvalid(this DependencyObject dependencyObject)
		{
			foreach (Control control in ViewHelper.FindChildren<Control>(dependencyObject))
			{
				control.SetInvalid(null, null);
			}
		}

		private static bool Compare(Control currentControl)
		{
			object resolvedSource;
			DependencyProperty dependencyProperty = ViewHelper.GetDependencyProperty(currentControl);
			if (dependencyProperty == null)
			{
				return false;
			}
			BindingExpression bindingExpression = currentControl.GetBindingExpression(dependencyProperty);
			if (bindingExpression != null)
			{
				resolvedSource = bindingExpression.ResolvedSource;
			}
			else
			{
				resolvedSource = null;
			}
			dynamic obj = resolvedSource;
			dynamic obj1 = obj != (dynamic)null;
			return (bool)((!obj1 ? obj1 : obj1 & (bool)obj.HasChange()));
		}

		public static void ControlLostFocus(object sender, RoutedEventArgs e)
		{
			dynamic obj;
			Control control;
			Control control1 = null;
			if (sender is CustomItemValidation)
			{
				control1 = ViewHelper.FindChildren<Control>(sender as CustomItemValidation).FirstOrDefault<Control>((Control currentControl) => {
					if (currentControl is PasswordBox || currentControl is TextBox || currentControl is ToggleButton || currentControl is DatePicker || currentControl is ComboBox || currentControl is AutoCompleteBox || currentControl is CheckComboBox || currentControl is Button)
					{
						return true;
					}
					return currentControl is CustomPasswordBox;
				});
			}
			if (sender is CustomIsReadOnlyControl)
			{
				CustomIsReadOnlyControl customIsReadOnlyControl = sender as CustomIsReadOnlyControl;
				if (!customIsReadOnlyControl.HasValidation)
				{
					control = null;
				}
				else
				{
					control = ViewHelper.FindChildren<Control>(customIsReadOnlyControl).LastOrDefault<Control>((Control currentControl) => {
						if (currentControl is PasswordBox || currentControl is TextBox || currentControl is ToggleButton || currentControl is DatePicker || currentControl is ComboBox || currentControl is AutoCompleteBox || currentControl is CheckComboBox)
						{
							return true;
						}
						return currentControl is Button;
					});
				}
				control1 = control;
			}
			if (control1 == null)
			{
				return;
			}
			ComboBox comboBox = control1 as ComboBox;
			if (comboBox != null && comboBox.IsDropDownOpen)
			{
				return;
			}
			if (control1 is TextBox && ((TextBox)control1).IsReadOnly)
			{
				return;
			}
			DependencyProperty dependencyProperty = ViewHelper.GetDependencyProperty(control1);
			if (dependencyProperty == null)
			{
				return;
			}
			BindingExpression bindingExpression = control1.GetBindingExpression(dependencyProperty);
			if (bindingExpression == null)
			{
				return;
			}
			Validation.ClearInvalid(bindingExpression);
			List<string> list = bindingExpression.ParentBinding.Path.Path.Split(new char[] { '.' }).ToList<string>();
			Type type = bindingExpression.DataItem.GetType();
			if (!list.Any<string>())
			{
				return;
			}
			PropertyInfo property = type.GetProperty(list.First<string>());
			if (property == null)
			{
				return;
			}
			dynamic value = property.GetValue(bindingExpression.DataItem, null);
			List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();
			if ((dynamic)(!ViewHelper.HasMethod(value, "ValidationEvent")))
			{
				dynamic resolvedSource = bindingExpression.ResolvedSource;
				if (resolvedSource is Control)
				{
					return;
				}
				if ((dynamic)(!ViewHelper.HasMethod(resolvedSource, "ValidationEvent")))
				{
					return;
				}
				obj = resolvedSource;
				if ((obj != null ? obj.ValidationEvent : null) == (dynamic)null)
				{
					return;
				}
				Func<List<KeyValuePair<string, string>>> func = resolvedSource.ValidationEvent as Func<List<KeyValuePair<string, string>>>;
				if (func != null)
				{
					keyValuePairs = func();
				}
				if (keyValuePairs.Count == 0)
				{
					return;
				}
			}
			if (keyValuePairs.Count == 0)
			{
				obj = value;
				if ((obj != null ? obj.ValidationEvent : null) == (dynamic)null)
				{
					return;
				}
				Func<List<KeyValuePair<string, string>>> func1 = value.ValidationEvent as Func<List<KeyValuePair<string, string>>>;
				if (func1 != null)
				{
					keyValuePairs = func1();
				}
			}
			if (keyValuePairs == null || !keyValuePairs.Any<KeyValuePair<string, string>>())
			{
				return;
			}
			string str = (list.Last<string>() == "Id" ? list[list.Count - 2] : list.Last<string>());
			string valueExact = ValidationHelper.GetValueExact(keyValuePairs, str);
			if (string.IsNullOrEmpty(valueExact))
			{
				valueExact = ValidationHelper.GetValueExact(keyValuePairs, str);
				if (string.IsNullOrEmpty(valueExact))
				{
					return;
				}
			}
			Validation.MarkInvalid(bindingExpression, new ValidationError(new ExceptionValidationRule(), bindingExpression)
			{
				ErrorContent = valueExact
			});
		}

		private static void DefaultBinding<T>(CheckComboBox comboBox, bool withSelect, bool enumDefault, string defaultValue = "", string defaultText = "", bool orderByLabel = false)
		{
			List<KeyValuePair<string, string>> keyValuePairs = BindingHelper.BindingData<T>(withSelect, enumDefault, defaultValue, defaultText, orderByLabel, false, false);
			comboBox.ItemsSource = keyValuePairs;
			comboBox.set_ValueMemberPath("Key");
			comboBox.DisplayMemberPath = "Value";
			comboBox.set_SelectedItem(keyValuePairs.First<KeyValuePair<string, string>>());
		}

		public static IEnumerable<T> FindAncestor<T>(DependencyObject dependencyObject)
		where T : DependencyObject
		{
			for (DependencyObject i = VisualTreeHelper.GetParent(dependencyObject); i != null; i = VisualTreeHelper.GetParent(i))
			{
				T t = (T)(i as T);
				if (t != null)
				{
					yield return t;
				}
			}
		}

		public static IEnumerable<T> FindChildren<T>(DependencyObject depObj)
		where T : DependencyObject
		{
			if (depObj != null)
			{
				IEnumerator enumerator = LogicalTreeHelper.GetChildren(depObj).GetEnumerator();
				while (enumerator.MoveNext())
				{
					object current = enumerator.Current;
					T t = (T)(current as T);
					if (t != null)
					{
						ContentControl contentControl = (object)t as ContentControl;
						if (contentControl == null || (object)t as UserControl != null)
						{
							yield return t;
						}
					}
					foreach (T t1 in ViewHelper.FindChildren<T>(current as DependencyObject))
					{
						yield return t1;
					}
					current = null;
				}
				enumerator = null;
			}
			else
			{
			}
		}

		public static IEnumerable<T> FindParent<T>(DependencyObject depObj)
		where T : DependencyObject
		{
			if (depObj != null)
			{
				DependencyObject parent = LogicalTreeHelper.GetParent(depObj);
				T t = (T)(parent as T);
				if (t != null)
				{
					yield return t;
				}
				foreach (T t1 in ViewHelper.FindParent<T>(parent))
				{
					yield return t1;
				}
			}
			else
			{
			}
		}

		private static DependencyProperty GetDependencyProperty(Control currentControl)
		{
			DependencyProperty selectedValueProperty = null;
			if (currentControl is CheckComboBox)
			{
				selectedValueProperty = Xceed.Wpf.Toolkit.Primitives.Selector.SelectedValueProperty;
			}
			if (currentControl is DatePicker)
			{
				selectedValueProperty = DatePicker.SelectedDateProperty;
			}
			if (currentControl is AutoCompleteBox)
			{
				selectedValueProperty = AutoCompleteBox.SelectedItemProperty;
			}
			if (currentControl is TextBox)
			{
				selectedValueProperty = TextBox.TextProperty;
			}
			if (currentControl is MaskedTextBox)
			{
				selectedValueProperty = TextBox.TextProperty;
			}
			if (currentControl is ComboBox)
			{
				selectedValueProperty = System.Windows.Controls.Primitives.Selector.SelectedValueProperty;
			}
			if (currentControl is CurrencyTextBox)
			{
				selectedValueProperty = CurrencyTextBox.NumberProperty;
			}
			if (currentControl is ToggleButton)
			{
				selectedValueProperty = ToggleButton.IsCheckedProperty;
			}
			if (currentControl is CustomPasswordBox)
			{
				selectedValueProperty = CustomPasswordBox.TextProperty;
			}
			return selectedValueProperty;
		}

		public static bool HasMethod(object objectToCheck, string methodName)
		{
			if (objectToCheck == null)
			{
				return false;
			}
			return objectToCheck.GetType().GetProperty(methodName) != null;
		}

		public static bool IsPropertyExist(dynamic settings, string name)
		{
			if (settings is ExpandoObject)
			{
				return ((IDictionary<string, object>)settings).ContainsKey(name);
			}
			return (bool)(settings.GetType().GetProperty(name) != (dynamic)null);
		}

		public static bool IsValid(this DependencyObject obj)
		{
			if (Validation.GetHasError(obj))
			{
				return false;
			}
			return LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All<DependencyObject>(new Func<DependencyObject, bool>(ViewHelper.IsValid));
		}

		public static bool IsWindowOpen<T>(string name = "")
		where T : Window
		{
			if (string.IsNullOrEmpty(name))
			{
				return System.Windows.Application.Current.Windows.OfType<T>().Any<T>();
			}
			return System.Windows.Application.Current.Windows.OfType<T>().Any<T>((T w) => w.Name.Equals(name));
		}

		public static Control SetInvalid(this Control currentControl, List<KeyValuePair<string, string>> errorMessages = null, string specificKey = null)
		{
			DependencyProperty dependencyProperty = ViewHelper.GetDependencyProperty(currentControl);
			if (dependencyProperty == null)
			{
				return null;
			}
			BindingExpression bindingExpression = currentControl.GetBindingExpression(dependencyProperty);
			if (errorMessages == null)
			{
				dynamic obj = (bindingExpression != null ? bindingExpression.ResolvedSource : null);
				if (obj is Control)
				{
					return null;
				}
				if ((dynamic)(!ViewHelper.HasMethod(obj, "ValidationEvent")))
				{
					return null;
				}
				dynamic obj1 = obj;
				if ((obj1 != null ? obj1.ValidationEvent : null) == (dynamic)null)
				{
					return null;
				}
				Func<List<KeyValuePair<string, string>>> func = obj.ValidationEvent as Func<List<KeyValuePair<string, string>>>;
				if (func != null)
				{
					errorMessages = func();
				}
			}
			if (bindingExpression == null)
			{
				return null;
			}
			BindingExpressionBase bindingExpressionBase = BindingOperations.GetBindingExpressionBase(currentControl, dependencyProperty);
			if (bindingExpressionBase == null)
			{
				return null;
			}
			Validation.ClearInvalid(bindingExpressionBase);
			if (errorMessages == null || !errorMessages.Any<KeyValuePair<string, string>>())
			{
				return null;
			}
			string[] strArrays = bindingExpression.ParentBinding.Path.Path.Split(new char[] { '.' });
			string str = specificKey ?? (strArrays.Last<string>() == "Id" ? strArrays[(int)strArrays.Length - 2] : strArrays.Last<string>());
			string valueExact = ValidationHelper.GetValueExact(errorMessages, str);
			if (string.IsNullOrEmpty(valueExact))
			{
				valueExact = ValidationHelper.GetValueExact(errorMessages, str);
				if (string.IsNullOrEmpty(valueExact))
				{
					return null;
				}
			}
			Validation.MarkInvalid(bindingExpressionBase, new ValidationError(new ExceptionValidationRule(), bindingExpression)
			{
				ErrorContent = valueExact
			});
			return currentControl;
		}

		public static void SetInvalid(Control control, string error, bool valid)
		{
			DependencyProperty dependencyProperty = ViewHelper.GetDependencyProperty(control);
			if (dependencyProperty == null)
			{
				return;
			}
			BindingExpression bindingExpression = control.GetBindingExpression(dependencyProperty);
			if (bindingExpression == null)
			{
				return;
			}
			ValidationError validationError = new ValidationError(new ExceptionValidationRule(), bindingExpression)
			{
				ErrorContent = error
			};
			BindingExpressionBase bindingExpressionBase = BindingOperations.GetBindingExpressionBase(control, dependencyProperty);
			if (bindingExpressionBase == null)
			{
				return;
			}
			Validation.ClearInvalid(bindingExpressionBase);
			if (valid)
			{
				return;
			}
			Validation.MarkInvalid(bindingExpressionBase, validationError);
		}

		public static void ValidateFields(this DependencyObject dependencyObject, List<KeyValuePair<string, string>> errorMessages, bool focusField = true)
		{
			if ((errorMessages == null ? true : !errorMessages.Any<KeyValuePair<string, string>>()))
			{
				dependencyObject.ClearInvalid();
				return;
			}
			Control control = null;
			foreach (Control control1 in ViewHelper.FindChildren<Control>(dependencyObject))
			{
				Control control2 = control1.SetInvalid(errorMessages, null);
				if (control != null || control2 == null)
				{
					continue;
				}
				control = control2;
			}
			if (focusField && control != null)
			{
				control.Focus();
			}
		}

		public static T Window<T>()
		where T : Window
		{
			return System.Windows.Application.Current.Windows.OfType<T>().FirstOrDefault<T>();
		}
	}
}