码迷,mamicode.com
首页 > 其他好文 > 详细

GotFocus和PreviewLeftButtonDown事件

时间:2014-11-05 00:03:43      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   使用   sp   div   on   log   

当TextBox获得焦点后,其中的文字会被全选。通过GotFocus和PreviewLeftButtonDown事件,就可以模拟上述行为。

如果用户只是用键盘操作,GotFocus事件就足够了。

如果使用鼠标操作,就要用到2个事件了。TextBox会将光标放在鼠标单击的那个位置。为了防止这种情况,会实现PreviewLeftButtonDown事件,

在该控件获得焦点后,将该事件的Handled属性设置为true。这会终止该事件,从而防止光标被定位到TextBox中的特定位置。

private void timeAllowedTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            timeAllowedTextBox.SelectAll();
        }

        private void timeAllowedTextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var control = sender as TextBox;
            if (control == null)
                return;

            Keyboard.Focus(control);
            e.Handled = true;
        }

 

GotFocus和PreviewLeftButtonDown事件

标签:style   blog   color   ar   使用   sp   div   on   log   

原文地址:http://www.cnblogs.com/tirmer/p/4075116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!