summaryrefslogtreecommitdiff
path: root/Gestor.Application/Helpers/ViewHelper.cs
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:35:25 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:35:25 +0000
commit674ca83ba9243a9e95a7568c797668dab6aee26a (patch)
tree4a905b3fb1d827665a34d63f67bc5559f8e7235b /Gestor.Application/Helpers/ViewHelper.cs
downloadgestor-674ca83ba9243a9e95a7568c797668dab6aee26a.tar.gz
gestor-674ca83ba9243a9e95a7568c797668dab6aee26a.zip
feat: upload files
Diffstat (limited to 'Gestor.Application/Helpers/ViewHelper.cs')
-rw-r--r--Gestor.Application/Helpers/ViewHelper.cs462
1 files changed, 462 insertions, 0 deletions
diff --git a/Gestor.Application/Helpers/ViewHelper.cs b/Gestor.Application/Helpers/ViewHelper.cs
new file mode 100644
index 0000000..9c17374
--- /dev/null
+++ b/Gestor.Application/Helpers/ViewHelper.cs
@@ -0,0 +1,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>();
+ }
+ }
+} \ No newline at end of file