diff options
Diffstat (limited to 'Codemerx/Gestor.Application/Helpers/BindingHelper.cs')
| -rw-r--r-- | Codemerx/Gestor.Application/Helpers/BindingHelper.cs | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Application/Helpers/BindingHelper.cs b/Codemerx/Gestor.Application/Helpers/BindingHelper.cs new file mode 100644 index 0000000..3c9d76e --- /dev/null +++ b/Codemerx/Gestor.Application/Helpers/BindingHelper.cs @@ -0,0 +1,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;
+ }
+ }
+}
\ No newline at end of file |