How to keep WPF TextBox selection when not focused?(不集中时如何保持WPF文本框选择?)
问题描述
我想在 WPF 文本框中显示一个选择,即使它不在焦点上.我该怎么做?
I want to show a selection in a WPF TextBox even when it's not in focus. How can I do this?
推荐答案
我已经将此解决方案用于 RichTextBox,但我认为它也适用于标准文本框.基本上,您需要处理 LostFocus 事件并将其标记为已处理.
I have used this solution for a RichTextBox, but I assume it will also work for a standard text box. Basically, you need to handle the LostFocus event and mark it as handled.
protected void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
// When the RichTextBox loses focus the user can no longer see the selection.
// This is a hack to make the RichTextBox think it did not lose focus.
e.Handled = true;
}
TextBox 不会意识到它失去了焦点,仍然会显示突出显示的选择.
The TextBox will not realize it lost the focus and will still show the highlighted selection.
在这种情况下我没有使用数据绑定,所以这可能会弄乱双向绑定.您可能必须在 LostFocus 事件处理程序中强制绑定.像这样的:
I'm not using data binding in this case, so it may be possible that this will mess up the two way binding. You may have to force binding in your LostFocus event handler. Something like this:
Binding binding = BindingOperations.GetBinding(this, TextProperty);
if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default ||
binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
{
BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
}
这篇关于不集中时如何保持WPF文本框选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不集中时如何保持WPF文本框选择?


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01