如何自动选择WPF文本框上的焦点的所有文本?文本框、文本、焦点、WPF

2023-09-02 01:24:19 作者:你就像条狗゛谁骚跟谁走

如果我称之为全选的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.