在WPF文本框上调用Select()不会执行任何操作

时间:2021-11-06 17:00:51

I'm using a WPF TextBox, and I'm calling the Select() method to make a selection. Unfortunately, it doesn't seem to work unless I select something manually first. Can someone suggest as to why it happens?

我正在使用WPF TextBox,我正在调用Select()方法进行选择。不幸的是,除非我先手动选择,否则它似乎不起作用。有人可以建议为什么会这样吗?

1 个解决方案

#1


10  

Does your textbox have focus when you call the Select method?

调用Select方法时,文本框是否具有焦点?

The selection will not show unless it has focus, but the common way for the textbox to get focus is by clicking on it...which deselects the text. This behaviour could give the impression that it's not working.

除非它具有焦点,否则选择将不会显示,但文本框获得焦点的常用方法是单击它...取消选择文本。这种行为可能会给人一种不起作用的印象。

Calling TextBox.Focus() after (or before) calling Select() should fix it.

在调用Select()之后(或之前)调用TextBox.Focus()应该修复它。

#1


10  

Does your textbox have focus when you call the Select method?

调用Select方法时,文本框是否具有焦点?

The selection will not show unless it has focus, but the common way for the textbox to get focus is by clicking on it...which deselects the text. This behaviour could give the impression that it's not working.

除非它具有焦点,否则选择将不会显示,但文本框获得焦点的常用方法是单击它...取消选择文本。这种行为可能会给人一种不起作用的印象。

Calling TextBox.Focus() after (or before) calling Select() should fix it.

在调用Select()之后(或之前)调用TextBox.Focus()应该修复它。