summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/Helpers/BindingHelper.cs
blob: 3c9d76eef0950cfb544077f9e9033216fc112d4e (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
using CurrencyTextBoxControl;
using Gestor.Common.Helpers;
using Gestor.Model.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;

namespace Gestor.Application.Helpers
{
	public static class BindingHelper
	{
		public static void AddBinding(object control, string path, ValidationRule rule = null)
		{
			Type type = control.GetType();
			Binding binding = new Binding()
			{
				Mode = BindingMode.TwoWay,
				UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
				Path = new PropertyPath(path, Array.Empty<object>())
			};
			if (rule != null)
			{
				binding.ValidationRules.Add(rule);
				binding.ValidationRules[0].ValidatesOnTargetUpdated = true;
			}
			string name = type.Name;
			if (name == "TextBox")
			{
				((TextBox)control).SetBinding(TextBox.TextProperty, binding);
				return;
			}
			if (name == "DatePicker")
			{
				((DatePicker)control).SetBinding(DatePicker.SelectedDateProperty, binding);
				return;
			}
			if (name != "CurrencyTextBox")
			{
				return;
			}
			((CurrencyTextBox)control).SetBinding(CurrencyTextBox.NumberProperty, binding);
		}

		public static void AddValidationRule(object control, ValidationRule rule = null)
		{
			// 
			// Current member / type: System.Void Gestor.Application.Helpers.BindingHelper::AddValidationRule(System.Object,System.Windows.Controls.ValidationRule)
			// File path: C:\AggerSeguros\Gestor.Application.exe
			// 
			// Product version: 0.0.0.0
			// Exception in: System.Void AddValidationRule(System.Object,System.Windows.Controls.ValidationRule)
			// 
			// Object reference not set to an instance of an object.
			//    at Telerik.JustDecompiler.Decompiler.TypeInference.TypeInferer.FindLowestCommonAncestor(ICollection`1 typeNodes) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\TypeInference\TypeInferer.cs:line 515
			//    at Telerik.JustDecompiler.Decompiler.TypeInference.TypeInferer.MergeWithLowestCommonAncestor() in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\TypeInference\TypeInferer.cs:line 459
			//    at Telerik.JustDecompiler.Decompiler.TypeInference.TypeInferer.ProcessSingleConstraints() in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\TypeInference\TypeInferer.cs:line 378
			//    at Telerik.JustDecompiler.Decompiler.TypeInference.TypeInferer.InferTypes() in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\TypeInference\TypeInferer.cs:line 331
			//    at Telerik.JustDecompiler.Decompiler.ExpressionDecompilerStep.Process(DecompilationContext theContext, BlockStatement body) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\ExpressionDecompilerStep.cs:line 104
			//    at Telerik.JustDecompiler.Decompiler.DecompilationPipeline.RunInternal(MethodBody body, BlockStatement block, ILanguage language) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\DecompilationPipeline.cs:line 100
			//    at Telerik.JustDecompiler.Decompiler.DecompilationPipeline.Run(MethodBody body, ILanguage language) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\DecompilationPipeline.cs:line 72
			//    at Telerik.JustDecompiler.Decompiler.Extensions.Decompile(MethodBody body, ILanguage language, DecompilationContext& context, TypeSpecificContext typeContext) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\Extensions.cs:line 61
			//    at Telerik.JustDecompiler.Decompiler.WriterContextServices.BaseWriterContextService.DecompileMethod(ILanguage language, MethodDefinition method, TypeSpecificContext typeContext) in D:\a\CodemerxDecompile\CodemerxDecompile\src\JustDecompileEngine\src\JustDecompiler.Shared\Decompiler\WriterContextServices\BaseWriterContextService.cs:line 133
			// 
			// mailto: JustDecompilePublicFeedback@telerik.com

		}

		private static List<KeyValuePair<string, string>> AfterTreatment<T>(bool withSelect, bool orderByLabel, List<KeyValuePair<string, string>> comboData, List<KeyValuePair<string, string>> comboValues, bool orderbyAttr = false)
		{
			List<KeyValuePair<string, string>> list = comboData;
			if (!withSelect)
			{
				list.RemoveAt(0);
			}
			list.AddRange(comboValues);
			if (orderbyAttr)
			{
				Type type = typeof(T);
				list = list.OrderBy<KeyValuePair<string, string>, int?>((KeyValuePair<string, string> s) => {
					if (s.Key == string.Empty)
					{
						return new int?(0);
					}
					return EnumHelper.GetOrder<T>((T)Enum.Parse(type, s.Key));
				}).ToList<KeyValuePair<string, string>>();
				return list;
			}
			if (!orderByLabel)
			{
				return list;
			}
			list = (
				from  in list
				orderby Math.Sign(ValidationHelper.ToFloat(x.Key)), Math.Abs(ValidationHelper.ToFloat(x.Key))
				select ).ToList<KeyValuePair<string, string>>();
			list = (
				from  in list
				orderby list.IndexOf(s) != 0, s.Value
				select ).ToList<KeyValuePair<string, string>>();
			return list;
		}

		private static List<KeyValuePair<string, string>> BeforeTreatment<T>(string defaultValue, string defaultText)
		{
			defaultText = (defaultText == string.Empty ? "Selecione" : defaultText);
			return new List<KeyValuePair<string, string>>()
			{
				new KeyValuePair<string, string>(defaultValue, defaultText)
			};
		}

		public static List<KeyValuePair<string, string>> BindingData<T>(bool withSelect = true, bool enumDefault = true, string defaultValue = "", string defaultText = "", bool orderByLabel = false, bool showAll = false, bool orderbyAttr = false)
		{
			List<KeyValuePair<string, string>> keyValuePairs = BindingHelper.BeforeTreatment<T>(defaultValue, defaultText);
			Type type = typeof(T);
			List<KeyValuePair<string, string>> list = Enum.GetNames(type).AsEnumerable<string>().Where<string>((string x) => {
				if (showAll)
				{
					return true;
				}
				T t = (T)Enum.Parse(type, x);
				bool? @default = EnumHelper.GetDefault<T>(t);
				bool hidden = EnumHelper.GetHidden<T>(t);
				if (@default.HasValue && @default.Value != enumDefault)
				{
					return false;
				}
				return !hidden;
			}).Select<string, KeyValuePair<string, string>>((string x) => {
				T t = (T)Enum.Parse(type, x);
				return new KeyValuePair<string, string>(EnumHelper.Value<T>(t), EnumHelper.GetDescription<T>(t));
			}).ToList<KeyValuePair<string, string>>();
			return BindingHelper.AfterTreatment<T>(withSelect, orderByLabel, keyValuePairs, list, orderbyAttr);
		}

		public static void DeleteValidationRule(object control)
		{
			Binding binding;
			string name = control.GetType().Name;
			if (name == "TextBox")
			{
				binding = BindingOperations.GetBinding((TextBox)control, TextBox.TextProperty) ?? new Binding()
				{
					Mode = BindingMode.TwoWay,
					UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
				};
				binding.ValidationRules.Clear();
				((TextBox)control).SetBinding(TextBox.TextProperty, binding);
				return;
			}
			if (name == "DatePicker")
			{
				binding = BindingOperations.GetBinding((DatePicker)control, DatePicker.SelectedDateProperty) ?? new Binding()
				{
					Mode = BindingMode.TwoWay,
					UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
				};
				binding.ValidationRules.Clear();
				((DatePicker)control).SetBinding(DatePicker.SelectedDateProperty, binding);
				return;
			}
			if (name != "CurrencyTextBox")
			{
				return;
			}
			binding = BindingOperations.GetBinding((CurrencyTextBox)control, CurrencyTextBox.NumberProperty) ?? new Binding()
			{
				Mode = BindingMode.TwoWay,
				UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
			};
			binding.ValidationRules.Clear();
			((CurrencyTextBox)control).SetBinding(CurrencyTextBox.NumberProperty, binding);
		}

		public static string GetPath(object control)
		{
			Binding binding;
			PropertyPath path;
			string name = control.GetType().Name;
			if (name == "TextBox")
			{
				binding = BindingOperations.GetBinding((TextBox)control, TextBox.TextProperty);
				if (binding == null)
				{
					return "";
				}
				path = binding.Path;
			}
			else if (name == "DatePicker")
			{
				binding = BindingOperations.GetBinding((DatePicker)control, DatePicker.SelectedDateProperty);
				if (binding == null)
				{
					return "";
				}
				path = binding.Path;
			}
			else if (name == "CurrencyTextBox")
			{
				binding = BindingOperations.GetBinding((CurrencyTextBox)control, CurrencyTextBox.NumberProperty);
				if (binding == null)
				{
					return "";
				}
				path = binding.Path;
			}
			else if (name == "ComboBox")
			{
				binding = BindingOperations.GetBinding((ComboBox)control, Selector.SelectedItemProperty);
				if (binding == null)
				{
					return "";
				}
				path = binding.Path;
			}
			else
			{
				if (name != "AutoCompleteBox")
				{
					return "";
				}
				binding = BindingOperations.GetBinding((AutoCompleteBox)control, AutoCompleteBox.SelectedItemProperty);
				if (binding == null)
				{
					return "";
				}
				path = binding.Path;
			}
			return path.Path;
		}
	}
}