C# 如何在没有焦点的情况下突出显示/选择 wpf 文本框中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12094937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:03:30  来源:igfitidea点击:

how to highlight/select text in a wpf textbox without focus?

c#wpftextboxselectedtext

提问by user1340852

I want to highlight selected text in a wpf textbox while the textbox is not focused. In my application, my textbox never gets focus, and every key input is done manually.

我想在文本框未聚焦时突出显示 wpf 文本框中的选定文本。在我的应用程序中,我的文本框永远不会获得焦点,并且每个键输入都是手动完成的。

I was wondering if there is a way to highlight the selected text when the textbox is not focused?

我想知道当文本框没有聚焦时是否有办法突出显示所选文本?

Any help would be appreciated!

任何帮助,将不胜感激!

回答by pdvries

You can use the following code to achieve your purpose:

您可以使用以下代码来实现您的目的:

textBoxToHighlight.Focus();
textBoxToHighlight.Select(0, textBoxToHighlight.Text.Length);

Hope this helps.

希望这可以帮助。

回答by usefulBee

Another alternative:

另一种选择:

textBoxName.SelectAll();