diff options
Diffstat (limited to 'Decompiler/Gestor.Application.Helpers/BindingHelper.cs')
| -rw-r--r-- | Decompiler/Gestor.Application.Helpers/BindingHelper.cs | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/Decompiler/Gestor.Application.Helpers/BindingHelper.cs b/Decompiler/Gestor.Application.Helpers/BindingHelper.cs new file mode 100644 index 0000000..cece7c8 --- /dev/null +++ b/Decompiler/Gestor.Application.Helpers/BindingHelper.cs @@ -0,0 +1,327 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Data; +using CurrencyTextBoxControl; +using Gestor.Common.Helpers; +using Gestor.Common.Validation; +using Gestor.Model.Helper; + +namespace Gestor.Application.Helpers; + +public static class BindingHelper +{ + public static void DeleteValidationRule(object control) + { + //IL_0038: Unknown result type (might be due to invalid IL or missing references) + //IL_0047: Expected O, but got Unknown + //IL_006b: Unknown result type (might be due to invalid IL or missing references) + //IL_004b: Unknown result type (might be due to invalid IL or missing references) + //IL_0050: Unknown result type (might be due to invalid IL or missing references) + //IL_0057: Unknown result type (might be due to invalid IL or missing references) + //IL_007e: Unknown result type (might be due to invalid IL or missing references) + //IL_008d: Expected O, but got Unknown + //IL_00b1: Unknown result type (might be due to invalid IL or missing references) + //IL_0091: Unknown result type (might be due to invalid IL or missing references) + //IL_0096: Unknown result type (might be due to invalid IL or missing references) + //IL_009d: Unknown result type (might be due to invalid IL or missing references) + //IL_00c4: Unknown result type (might be due to invalid IL or missing references) + //IL_00d3: Expected O, but got Unknown + //IL_00f7: Unknown result type (might be due to invalid IL or missing references) + //IL_00d7: Unknown result type (might be due to invalid IL or missing references) + //IL_00dc: Unknown result type (might be due to invalid IL or missing references) + //IL_00e3: Unknown result type (might be due to invalid IL or missing references) + switch (control.GetType().Name) + { + case "TextBox": + { + Binding val = (Binding)(((object)BindingOperations.GetBinding((DependencyObject)(TextBox)control, TextBox.TextProperty)) ?? ((object)new Binding + { + Mode = (BindingMode)0, + UpdateSourceTrigger = (UpdateSourceTrigger)1 + })); + val.ValidationRules.Clear(); + ((FrameworkElement)(TextBox)control).SetBinding(TextBox.TextProperty, (BindingBase)(object)val); + break; + } + case "DatePicker": + { + Binding val = (Binding)(((object)BindingOperations.GetBinding((DependencyObject)(DatePicker)control, DatePicker.SelectedDateProperty)) ?? ((object)new Binding + { + Mode = (BindingMode)0, + UpdateSourceTrigger = (UpdateSourceTrigger)1 + })); + val.ValidationRules.Clear(); + ((FrameworkElement)(DatePicker)control).SetBinding(DatePicker.SelectedDateProperty, (BindingBase)(object)val); + break; + } + case "CurrencyTextBox": + { + Binding val = (Binding)(((object)BindingOperations.GetBinding((DependencyObject)(CurrencyTextBox)control, CurrencyTextBox.NumberProperty)) ?? ((object)new Binding + { + Mode = (BindingMode)0, + UpdateSourceTrigger = (UpdateSourceTrigger)1 + })); + val.ValidationRules.Clear(); + ((FrameworkElement)(CurrencyTextBox)control).SetBinding(CurrencyTextBox.NumberProperty, (BindingBase)(object)val); + break; + } + } + } + + public static string GetPath(object control) + { + //IL_005a: Unknown result type (might be due to invalid IL or missing references) + //IL_0069: Expected O, but got Unknown + //IL_0080: Unknown result type (might be due to invalid IL or missing references) + //IL_008f: Expected O, but got Unknown + //IL_00a3: Unknown result type (might be due to invalid IL or missing references) + //IL_00b2: Expected O, but got Unknown + //IL_00c6: Unknown result type (might be due to invalid IL or missing references) + //IL_00d5: Expected O, but got Unknown + //IL_00e9: Unknown result type (might be due to invalid IL or missing references) + //IL_00f8: Expected O, but got Unknown + PropertyPath path; + switch (control.GetType().Name) + { + default: + return ""; + case "TextBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(TextBox)control, TextBox.TextProperty); + if (binding == null) + { + return ""; + } + path = binding.Path; + break; + } + case "DatePicker": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(DatePicker)control, DatePicker.SelectedDateProperty); + if (binding == null) + { + return ""; + } + path = binding.Path; + break; + } + case "CurrencyTextBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(CurrencyTextBox)control, CurrencyTextBox.NumberProperty); + if (binding == null) + { + return ""; + } + path = binding.Path; + break; + } + case "ComboBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(ComboBox)control, Selector.SelectedItemProperty); + if (binding == null) + { + return ""; + } + path = binding.Path; + break; + } + case "AutoCompleteBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(AutoCompleteBox)control, AutoCompleteBox.SelectedItemProperty); + if (binding == null) + { + return ""; + } + path = binding.Path; + break; + } + } + return path.Path; + } + + public static void AddValidationRule(object control, ValidationRule rule = null) + { + //IL_0006: Unknown result type (might be due to invalid IL or missing references) + //IL_000b: Unknown result type (might be due to invalid IL or missing references) + //IL_0012: Unknown result type (might be due to invalid IL or missing references) + //IL_001a: Expected O, but got Unknown + //IL_004f: Unknown result type (might be due to invalid IL or missing references) + //IL_005e: Expected O, but got Unknown + //IL_00c3: Unknown result type (might be due to invalid IL or missing references) + //IL_00d2: Expected O, but got Unknown + //IL_0085: Unknown result type (might be due to invalid IL or missing references) + //IL_008f: Expected O, but got Unknown + //IL_013d: Unknown result type (might be due to invalid IL or missing references) + //IL_014c: Expected O, but got Unknown + //IL_00b0: Unknown result type (might be due to invalid IL or missing references) + //IL_00ff: Unknown result type (might be due to invalid IL or missing references) + //IL_0109: Expected O, but got Unknown + //IL_00e9: Unknown result type (might be due to invalid IL or missing references) + //IL_012a: Unknown result type (might be due to invalid IL or missing references) + //IL_0179: Unknown result type (might be due to invalid IL or missing references) + //IL_0183: Expected O, but got Unknown + //IL_0163: Unknown result type (might be due to invalid IL or missing references) + //IL_01a4: Unknown result type (might be due to invalid IL or missing references) + Type type = control.GetType(); + Binding val = new Binding + { + Mode = (BindingMode)0, + UpdateSourceTrigger = (UpdateSourceTrigger)1 + }; + switch (type.Name) + { + case "TextBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(TextBox)control, TextBox.TextProperty); + if (binding != null) + { + PropertyPath path = binding.Path; + if (rule != null) + { + val.ValidationRules.Add(rule); + } + val.Path = new PropertyPath(path.Path, Array.Empty<object>()); + if (val.ValidationRules.Count > 0) + { + val.ValidationRules[0].ValidatesOnTargetUpdated = true; + } + ((FrameworkElement)(TextBox)control).SetBinding(TextBox.TextProperty, (BindingBase)(object)val); + } + break; + } + case "DatePicker": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(DatePicker)control, DatePicker.SelectedDateProperty); + if (binding != null) + { + PropertyPath path = binding.Path; + val.ValidationRules.Add((ValidationRule)(((object)rule) ?? ((object)new ValidacaoData()))); + val.Path = new PropertyPath(path.Path, Array.Empty<object>()); + if (val.ValidationRules.Count > 0) + { + val.ValidationRules[0].ValidatesOnTargetUpdated = true; + } + ((FrameworkElement)(DatePicker)control).SetBinding(DatePicker.SelectedDateProperty, (BindingBase)(object)val); + } + break; + } + case "CurrencyTextBox": + { + Binding binding = BindingOperations.GetBinding((DependencyObject)(CurrencyTextBox)control, CurrencyTextBox.NumberProperty); + if (binding != null) + { + PropertyPath path = binding.Path; + val.ValidationRules.Add((ValidationRule)(((object)rule) ?? ((object)new ValidacaoDouble()))); + val.Path = new PropertyPath(path.Path, Array.Empty<object>()); + if (val.ValidationRules.Count > 0) + { + val.ValidationRules[0].ValidatesOnTargetUpdated = true; + } + ((FrameworkElement)(CurrencyTextBox)control).SetBinding(CurrencyTextBox.NumberProperty, (BindingBase)(object)val); + } + break; + } + } + } + + public static void AddBinding(object control, string path, ValidationRule rule = null) + { + //IL_0006: Unknown result type (might be due to invalid IL or missing references) + //IL_000b: Unknown result type (might be due to invalid IL or missing references) + //IL_0012: Unknown result type (might be due to invalid IL or missing references) + //IL_0019: Unknown result type (might be due to invalid IL or missing references) + //IL_0020: Unknown result type (might be due to invalid IL or missing references) + //IL_002a: Expected O, but got Unknown + //IL_002b: Expected O, but got Unknown + //IL_007b: Unknown result type (might be due to invalid IL or missing references) + //IL_008e: Unknown result type (might be due to invalid IL or missing references) + //IL_00a1: Unknown result type (might be due to invalid IL or missing references) + Type type = control.GetType(); + Binding val = new Binding + { + Mode = (BindingMode)0, + UpdateSourceTrigger = (UpdateSourceTrigger)1, + Path = new PropertyPath(path, Array.Empty<object>()) + }; + if (rule != null) + { + val.ValidationRules.Add(rule); + val.ValidationRules[0].ValidatesOnTargetUpdated = true; + } + switch (type.Name) + { + case "TextBox": + ((FrameworkElement)(TextBox)control).SetBinding(TextBox.TextProperty, (BindingBase)(object)val); + break; + case "DatePicker": + ((FrameworkElement)(DatePicker)control).SetBinding(DatePicker.SelectedDateProperty, (BindingBase)(object)val); + break; + case "CurrencyTextBox": + ((FrameworkElement)(CurrencyTextBox)control).SetBinding(CurrencyTextBox.NumberProperty, (BindingBase)(object)val); + break; + } + } + + 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>> comboData = BeforeTreatment<T>(defaultValue, defaultText); + Type enumType = typeof(T); + List<KeyValuePair<string, string>> comboValues = Enum.GetNames(enumType).AsEnumerable().Where(delegate(string x) + { + if (showAll) + { + return true; + } + T obj = (T)Enum.Parse(enumType, x); + bool? @default = EnumHelper.GetDefault<T>(obj); + bool hidden = EnumHelper.GetHidden<T>(obj); + return (!@default.HasValue || @default.Value == enumDefault) && !hidden; + }) + .Select(delegate(string x) + { + T val = (T)Enum.Parse(enumType, x); + return new KeyValuePair<string, string>(EnumHelper.Value<T>(val), EnumHelper.GetDescription<T>(val)); + }) + .ToList(); + return AfterTreatment<T>(withSelect, orderByLabel, comboData, comboValues, orderbyAttr); + } + + 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) + }; + } + + 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) + { + if (!withSelect) + { + comboData.RemoveAt(0); + } + comboData.AddRange(comboValues); + if (orderbyAttr) + { + Type enumType = typeof(T); + comboData = comboData.OrderBy((KeyValuePair<string, string> s) => (s.Key == string.Empty) ? new int?(0) : EnumHelper.GetOrder<T>((T)Enum.Parse(enumType, s.Key))).ToList(); + return comboData; + } + if (!orderByLabel) + { + return comboData; + } + comboData = (from x in comboData + orderby Math.Sign(ValidationHelper.ToFloat(x.Key)), Math.Abs(ValidationHelper.ToFloat(x.Key)) + select x).ToList(); + comboData = (from s in comboData + orderby comboData.IndexOf(s) != 0, s.Value + select s).ToList(); + return comboData; + } +} |