简体   繁体   English

失去焦点后在 ElementHost 中的 WPF RichTextBox 中显示选择

[英]Show selection in WPF RichTextBox in ElementHost after losing focus

I'm currently developing a windows forms application (basically a document editor) with a WPF RichTextBox.我目前正在使用 WPF RichTextBox 开发 windows forms 应用程序(基本上是文档编辑器)。 Of course, since all the rest is a windows forms app, I host the WPF RichTextBox inside an ElementHost.当然,由于所有 rest 都是 windows forms 应用程序,因此我将 Z3055DD731D769EAA7189FZ RichTextBox3 托管在一个 Element 内。

The problem is that I want to keep the selection in the RichTextBox visible, even if it doesn't have focus.问题是我想让 RichTextBox 中的选择保持可见,即使它没有焦点。 For example, I have several text formatting buttons (bold, italic, ...) in the toolbar, and when I press one of them, I currently can't see the selection anymore because the RichTextBox loses focus.例如,我在工具栏中有几个文本格式按钮(粗体、斜体、...),当我按下其中一个时,我目前看不到选择,因为 RichTextBox 失去焦点。

Unfortunately, the RichTextBox doesn't have a HideSelection property like some WinForms controls.不幸的是,RichTextBox 没有像某些 WinForms 控件那样的 HideSelection 属性。

I have already tried cancelling the LostFocus event as described in other posts.我已经尝试过取消 LostFocus 事件,如其他帖子中所述。 However, this doesn't seem to work when the RichTextBox is hosted inside an ElementHost (I tried and it did work in a WPF-only project).但是,当 RichTextBox 托管在 ElementHost 中时,这似乎不起作用(我尝试过,它确实在仅 WPF 的项目中工作)。

I also experimented a bit with setting/resetting selection background colors when losing/gaining focus, but this seems very hackish and it's much effort to make it work reliably.在失去/获得焦点时,我还尝试了一些设置/重置选择背景 colors ,但这似乎很不自然,要使其可靠工作需要付出很多努力。

Does anyone have an idea how this could be achieved?有谁知道如何实现这一目标?

Thank you!谢谢!

I found a solution:我找到了一个解决方案:

Add a second RichTextBox to your ElementHost (with a height of 0).将第二个 RichTextBox 添加到您的 ElementHost(高度为 0)。
In the LostFocus of your RichTextBox do this:在 RichTextBox 的 LostFocus 中执行以下操作:

bool firstLost = true;

void yourRichTextBox_LostFocus(object sender, RoutedEventArgs e)
{
        e.Handled = true;

        if (firstLost)
        {
            yourRichTextBox.Focus();
            firstLost = false;
        }
        else
        {
            firstLost = true;
        }

        invisibleRichTextBox.Focus();
}

So it gets the Focus, gives it to the invisible RichTextBox (so the e.Handled = true; works correct) and then you can do whatever you want in your WinForms-controls.所以它得到了焦点,把它交给了不可见的 RichTextBox(所以e.Handled = true;工作正常),然后你可以在你的 WinForms 控件中做任何你想做的事情。

Does only work it the focus is set to a control of the same form - if you leave the parentform directly from your WPF-RichTextBox it loses the selection-highlight.仅在将焦点设置为相同表单的控件时才起作用-如果您直接从 WPF-RichTextBox 离开父表单,它将丢失选择突出显示。 (The LostFocus-Event seems not to be raised.) (LostFocus-Event 似乎没有引发。)

Edit: You can catch the Deactivate-event from your form and focus the yourRichTextBox and then focus the invisivleRichTextBox .编辑:您可以从表单中捕获 Deactivate-event 并关注yourRichTextBox ,然后关注invisivleRichTextBox So you con directly leave your form.所以你直接离开你的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM