using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using Tango.AutoComplete.Editors; using Tango.SharedUI; public static class ViewModelExtensionMethods { /// /// Sets the focus to the visual element that binds to the specified property. /// /// /// The view model. /// The property lambda. public static void SetFocus(this ViewModel vm, Expression> propertyLambda) { var me = propertyLambda.Body as MemberExpression; SetFocus(vm, me.Member.Name); } /// /// Sets the focus to the visual element that binds to the specified property. /// /// The view model. /// The property name. public static void SetFocus(this ViewModel vm, string propertyName) { Application.Current.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(200); var control = GetUserControl(vm, Application.Current.MainWindow); if (control != null) { var element = FindElementByPropertyName(vm, control, propertyName); if (element != null) { element.Focusable = true; Keyboard.Focus(element); } else { Debug.WriteLine($"SetFocus: {propertyName} not found!"); } } else { Debug.WriteLine($"SetFocus: {propertyName} not found!"); } }), DispatcherPriority.ApplicationIdle); } private static UserControl GetUserControl(ViewModel vm, FrameworkElement parent) { var children = parent.FindVisualChildren(); foreach (var child in children) { if (child.DataContext == vm) { return child; } else { var innerChild = GetUserControl(vm, child); if (innerChild != null) { return innerChild; } } } return null; } private static FrameworkElement FindElementByPropertyName(ViewModel vm, FrameworkElement parent, string propName) { FrameworkElement foundChild = null; var children = LogicalTreeHelper.GetChildren(parent).OfType().ToList(); int count = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement; if (child != null) { children.Add(child); } } children = children.Distinct().ToList(); foreach (var child in children) { if (child.DataContext == vm) { BindingExpression bindingExpression = child.GetBindingExpression(TextBox.TextProperty); if (bindingExpression == null) { bindingExpression = child.GetBindingExpression(Button.CommandProperty); } if (bindingExpression == null) { bindingExpression = child.GetBindingExpression(Tango.SharedUI.Helpers.PasswordHelper.PasswordProperty); } if (bindingExpression == null) { bindingExpression = child.GetBindingExpression(AutoCompleteTextBox.SelectedItemProperty); } if (bindingExpression != null) { if (bindingExpression.ResolvedSourcePropertyName == propName) { return child; } } } foundChild = FindElementByPropertyName(vm, child, propName); if (foundChild != null) { return foundChild; } } return foundChild; } }