WPF的TextBox的焦点获取与失去焦点的死循环解决方案

 

在WPF中实现一个弹出层自动获取焦点,弹出层实现是通过其UserControl的依赖属性Visibility的绑定实现的,让UserControl上的TextBox获取焦点,初始实现代码如下:

   
        public Visibility IsVisibile
        {
            get { return (Visibility)GetValue(IsVisibileProperty); }
            set { SetValue(IsVisibileProperty, value); }
        }
        public static readonly DependencyProperty IsVisibileProperty =
            DependencyProperty.Register("IsVisibile", typeof(Visibility), typeof(WordsKeyboard), new PropertyMetadata(Visibility.Collapsed,new PropertyChangedCallback((d,e)=>
                {
                    if((Visibility)e.NewValue==Visibility.Visibile)
                     (d as MainUserControl).textBox.Focus();
                  
                })));

但是第一次弹出该层的时候焦点未在textBox上,除了第一次弹出未获取到焦点后面的弹出都获取到了,为了解决第一次弹出层的TextBox未获取到焦点采用了定时器延时的方法,解决方案如下:

/// <summary>
        /// 定义个定时器
        /// </summary>
        DispatcherTimer timer = new DispatcherTimer();
        /// <summary>
        /// 构造方法
        /// </summary>
        public MainUserContrl()
        {
            InitializeComponent();
            timer.Interval = new TimeSpan(500);
            timer.Tick += timer_Tick;
        }

        /// <summary>
        /// 定时器间隔执行方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick(object sender, EventArgs e)
        {
            this.MainText.Focus();
            timer.Stop();
        }
 /// <summary>
        /// 控制改用户控件显示隐藏的依赖属性
        /// </summary>
        public Visibility IsVisibile
        {
            get { return (Visibility)GetValue(IsVisibileProperty); }
            set { SetValue(IsVisibileProperty, value); }
        }
        public static readonly DependencyProperty IsVisibileProperty =
            DependencyProperty.Register("IsVisibile", typeof(Visibility), typeof(WordsKeyboard), new PropertyMetadata(Visibility.Collapsed,new PropertyChangedCallback((d,e)=>
                {
                    if((Visibility)e.NewValue==Visibility.Collapsed)
                    (d as WordsKeyboard).KillKeyboard();
                    else
                    {
                        (d as MainUserControl).timer.Start();//开启定时器,让textBox获取到焦点
                    }
                })));

通过延时的方法就可以解决掉弹出层第一次无法获取焦点的问题了!而后面让textbox在能使用的情况下不能失去焦点(除非点击弹出层的关闭按钮,让弹出层消失),初始实现代码如下:

  /// <summary>
        /// textBox失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_LostFocus(object sender,RoutedEventArgs e)
        {
            if(textBox.IsEnabled&&!btnCancle.IsFocused)
            textBox.Focus();
        }

上述实现,会出现死循环,假如不出现死循环btnCancle.IsFocused的值一直是false,因为取消按钮一直没有获取到焦点!而解决方案实现如下:

 DispatcherTimer timerLoseFocuse = new DispatcherTimer();  
 timerLoseFocuse.Interval = new TimeSpan(0,0,0,0,100);
            timerLoseFocuse.Tick+=(s,e)=>
                {
                    if(CardNumArea.IsEnabled&&!BtnCancel.IsFocused)
                    {
                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,new Action(()=>
                        {
                            t.extBox.Focus();
                        }));//改方法能解决掉LoseFocuse出现死循环的问题
                    }
                    timerLoseFocuse.Stop();
                };
  /// <summary>
 ///失去焦点事件
  /// </summary>
 private void textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            timerLoseFocuse.Start();
           
        }

通过定时器解决取消按钮无法获取到获取到焦点的问题,否则点击取消无法执行其点击事件

WPF的TextBox的焦点获取与失去焦点的死循环解决方案

上一篇:GoogleServices 之 Location Location APIS翻译官方文档 (喷子勿喷,本人不会英语)


下一篇:Eclipse无法正常连接mysql8.0.20