如何自动选择WPF文本框上的焦点的所有文本? [英] How to automatically select all text on focus in WPF TextBox?

查看:525
本文介绍了如何自动选择WPF文本框上的焦点的所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我称之为全选的GotFocus 事件处理程序,它不使用鼠标的工作 - 选择一旦释放鼠标消失。

If I call SelectAll from a GotFocus event handler, it doesn't work with the mouse - the selection disappears as soon as mouse is released.

编辑:人们喜欢Donnelle的回答,我会尽力解释我为什么不喜欢它,就像接受的答案

People are liking Donnelle's answer, I'll try to explain why I did not like it as much as the accepted answer.

  • 这是比较复杂的,而公认的答案确实在一个更简单的方法是一样的。
  • 在接受答案的实用性更好。当您在文字中间点击,文字变得未选中当您松开鼠标让您立即开始编辑,如果你仍然想选择所有,只需preSS一次按钮,这一次也不会取消选择上推出。继Donelle的配方,如果我点击的文字中间,我必须单击第二次能够编辑。如果我点击的文本与文本外的某处,这很有可能意味着我要开始,而不是覆盖一切的编辑。

推荐答案

不知道为什么它失去了选择在GotFocus事件。

Don't know why it loses the selection in the GotFocus event.

但是一种解决方案是做在GotKeyboardFocus和GotMouseCapture事件的选择。这样,它永远是可行的。

But one solution is to do the selection on the GotKeyboardFocus and the GotMouseCapture events. That way it will always work.

这篇关于如何自动选择WPF文本框上的焦点的所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆