首页 > 其他 > 详细

GotFocus和PreviewLeftButtonDown事件

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

当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事件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!