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(this CheckComboBox comboBox, bool withSelect = true, bool enumDefault = true, string defaultValue = "", bool orderByLabel = false) { ViewHelper.DefaultBinding(comboBox, withSelect, enumDefault, defaultValue, string.Empty, orderByLabel); } public static void ClearInvalid(this DependencyObject dependencyObject) { foreach (Control control in ViewHelper.FindChildren(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(sender as CustomItemValidation).FirstOrDefault((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(customIsReadOnlyControl).LastOrDefault((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 list = bindingExpression.ParentBinding.Path.Path.Split(new char[] { '.' }).ToList(); Type type = bindingExpression.DataItem.GetType(); if (!list.Any()) { return; } PropertyInfo property = type.GetProperty(list.First()); if (property == null) { return; } dynamic value = property.GetValue(bindingExpression.DataItem, null); List> keyValuePairs = new List>(); 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>> func = resolvedSource.ValidationEvent as Func>>; 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>> func1 = value.ValidationEvent as Func>>; if (func1 != null) { keyValuePairs = func1(); } } if (keyValuePairs == null || !keyValuePairs.Any>()) { return; } string str = (list.Last() == "Id" ? list[list.Count - 2] : list.Last()); 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(CheckComboBox comboBox, bool withSelect, bool enumDefault, string defaultValue = "", string defaultText = "", bool orderByLabel = false) { List> keyValuePairs = BindingHelper.BindingData(withSelect, enumDefault, defaultValue, defaultText, orderByLabel, false, false); comboBox.ItemsSource = keyValuePairs; comboBox.set_ValueMemberPath("Key"); comboBox.DisplayMemberPath = "Value"; comboBox.set_SelectedItem(keyValuePairs.First>()); } public static IEnumerable FindAncestor(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 FindChildren(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(current as DependencyObject)) { yield return t1; } current = null; } enumerator = null; } else { } } public static IEnumerable FindParent(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(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)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().All(new Func(ViewHelper.IsValid)); } public static bool IsWindowOpen(string name = "") where T : Window { if (string.IsNullOrEmpty(name)) { return System.Windows.Application.Current.Windows.OfType().Any(); } return System.Windows.Application.Current.Windows.OfType().Any((T w) => w.Name.Equals(name)); } public static Control SetInvalid(this Control currentControl, List> 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>> func = obj.ValidationEvent as Func>>; 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>()) { return null; } string[] strArrays = bindingExpression.ParentBinding.Path.Path.Split(new char[] { '.' }); string str = specificKey ?? (strArrays.Last() == "Id" ? strArrays[(int)strArrays.Length - 2] : strArrays.Last()); 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> errorMessages, bool focusField = true) { if ((errorMessages == null ? true : !errorMessages.Any>())) { dependencyObject.ClearInvalid(); return; } Control control = null; foreach (Control control1 in ViewHelper.FindChildren(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() where T : Window { return System.Windows.Application.Current.Windows.OfType().FirstOrDefault(); } } }