Set focus on TextBox in WPF from view model

后端 未结 21 2698
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:21

I have a TextBox and a Button in my view.

Now I am checking a condition upon button click and if the condition turns out to be false, displ

21条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 06:54

    First off i would like to thank Avanka for helping me solve my focus problem. There is however a bug in the code he posted, namely in the line: if (e.OldValue == null)

    The problem I had was that if you first click in your view and focus the control, e.oldValue is no longer null. Then when you set the variable to focus the control for the first time, this results in the lostfocus and gotfocus handlers not being set. My solution to this was as follows:

    public static class ExtensionFocus
        {
        static ExtensionFocus()
            {
            BoundElements = new List();
            }
    
        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached("IsFocused", typeof(bool?),
            typeof(ExtensionFocus), new FrameworkPropertyMetadata(false, IsFocusedChanged));
    
        private static List BoundElements;
    
        public static bool? GetIsFocused(DependencyObject element)
            {
            if (element == null)
                {
                throw new ArgumentNullException("ExtensionFocus GetIsFocused called with null element");
                }
            return (bool?)element.GetValue(IsFocusedProperty);
            }
    
        public static void SetIsFocused(DependencyObject element, bool? value)
            {
            if (element == null)
                {
                throw new ArgumentNullException("ExtensionFocus SetIsFocused called with null element");
                }
            element.SetValue(IsFocusedProperty, value);
            }
    
        private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
            var fe = (FrameworkElement)d;
    
            // OLD LINE:
            // if (e.OldValue == null)
            // TWO NEW LINES:
            if (BoundElements.Contains(fe.Name) == false)
                {
                BoundElements.Add(fe.Name);
                fe.LostFocus += OnLostFocus;
                fe.GotFocus += OnGotFocus;
                }           
    
    
            if (!fe.IsVisible)
                {
                fe.IsVisibleChanged += new DependencyPropertyChangedEventHandler(fe_IsVisibleChanged);
                }
    
            if ((bool)e.NewValue)
                {
                fe.Focus();             
                }
            }
    
        private static void fe_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
            var fe = (FrameworkElement)sender;
    
            if (fe.IsVisible && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
                {
                fe.IsVisibleChanged -= fe_IsVisibleChanged;
                fe.Focus();
                }
            }
    
        private static void OnLostFocus(object sender, RoutedEventArgs e)
            {
            if (sender != null && sender is Control s)
                {
                s.SetValue(IsFocusedProperty, false);
                }
            }
    
        private static void OnGotFocus(object sender, RoutedEventArgs e)
            {
            if (sender != null && sender is Control s)
                {
                s.SetValue(IsFocusedProperty, true);
                }
            }
        }
    

提交回复
热议问题