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()) }; 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> AfterTreatment(bool withSelect, bool orderByLabel, List> comboData, List> comboValues, bool orderbyAttr = false) { List> list = comboData; if (!withSelect) { list.RemoveAt(0); } list.AddRange(comboValues); if (orderbyAttr) { Type type = typeof(T); list = list.OrderBy, int?>((KeyValuePair s) => { if (s.Key == string.Empty) { return new int?(0); } return EnumHelper.GetOrder((T)Enum.Parse(type, s.Key)); }).ToList>(); 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>(); list = ( from in list orderby list.IndexOf(s) != 0, s.Value select ).ToList>(); return list; } private static List> BeforeTreatment(string defaultValue, string defaultText) { defaultText = (defaultText == string.Empty ? "Selecione" : defaultText); return new List>() { new KeyValuePair(defaultValue, defaultText) }; } public static List> BindingData(bool withSelect = true, bool enumDefault = true, string defaultValue = "", string defaultText = "", bool orderByLabel = false, bool showAll = false, bool orderbyAttr = false) { List> keyValuePairs = BindingHelper.BeforeTreatment(defaultValue, defaultText); Type type = typeof(T); List> list = Enum.GetNames(type).AsEnumerable().Where((string x) => { if (showAll) { return true; } T t = (T)Enum.Parse(type, x); bool? @default = EnumHelper.GetDefault(t); bool hidden = EnumHelper.GetHidden(t); if (@default.HasValue && @default.Value != enumDefault) { return false; } return !hidden; }).Select>((string x) => { T t = (T)Enum.Parse(type, x); return new KeyValuePair(EnumHelper.Value(t), EnumHelper.GetDescription(t)); }).ToList>(); return BindingHelper.AfterTreatment(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; } } }