如何保持WPF文本框选择不集中的时候? [英] How to keep WPF TextBox selection when not focused?

查看:92
本文介绍了如何保持WPF文本框选择不集中的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在WPF的TextBox的选择,即使它没有焦点。我怎样才能做到这一点?

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;
  }

文本框不会意识到它失去焦点,并仍然会显示突出显示的选项。

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文本框选择不集中的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆