我想在 WPF TextBox 中显示一个选择,即使它没有焦点。我怎样才能做到这一点?
我已将此解决方案用于 RichTextBox,但我认为它也适用于标准文本框。基本上,您需要处理 LostFocus 事件并将其标记为已处理。
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;
}
文本框不会意识到它失去了焦点,并且仍会显示突出显示的选择。
在这种情况下,我没有使用数据绑定,因此这可能会扰乱双向绑定。您可能必须在 LostFocus 事件处理程序中强制绑定。像这样的东西:
Binding binding = BindingOperations.GetBinding(this, TextProperty);
if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default ||
binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
{
BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)