如何使用 WindowsFormHost 在 WPF 中聚焦

发布于 2024-10-03 14:03:18 字数 798 浏览 4 评论 0原文

我有一个简单的设置:

<StackPanel>
    <TextBox Text="wpf1" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
    <TextBox Text="wpf2" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
    <WindowsFormsHost>
        <wf:TextBox Text="winforms" />
    </WindowsFormsHost>
</StackPanel>

private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    e.Handled = true;
}

三个文本框、两个 WPF 和一个 WinForm。我无法在我想要的两个 WPF 文本框之间移动焦点,但我可以将焦点移动到 WinForm 文本框。从 WPF 文本框移动到 WinForm 文本框时,甚至不会触发 PreviewLostKeyboardFocus 事件。关于为什么以及如何解决这个问题的任何线索?

编辑

我注意到,当焦点再次离开 WindowsFormsHost 时,首先触发 WindowsFormsHost.PreviewGotKeyboardFocus 。太奇怪了。也许这是一个错误?

I have this simple setup:

<StackPanel>
    <TextBox Text="wpf1" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
    <TextBox Text="wpf2" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
    <WindowsFormsHost>
        <wf:TextBox Text="winforms" />
    </WindowsFormsHost>
</StackPanel>

private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    e.Handled = true;
}

Three textboxes, two WPF and one WinForm. I can't move focus between the two WPF-Textboxes which I desired but I can move focus to the WinForm-Textbox. The event PreviewLostKeyboardFocus does not even triggered when moving from a WPF-Textbox to a WinForm-Textbox. Any clues to why and how this could be solved?

EDIT

I've noticed that WindowsFormsHost.PreviewGotKeyboardFocus is triggered first when focus is leaving the WindowsFormsHost again. Thats odd. Maybe it's a bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文