WPF举办一个WinForm,标签导航问题标签、问题、WPF、WinForm

2023-09-04 23:42:34 作者:忧伤倒数幸福

我已经托管在的WindowsFormsHost 一个WinForms形式的标签导航时遇到问题。为了解决我做了这个简单的例子:

I have run into issues when hosting a WinForms form within a WindowsFormsHost and the tab navigation. To solve I have made this simple example:

创建WPF 窗口(起点应用程序的) 创建的WinForms 表格有两个文本框它 在WPF窗口:新增的WindowsFormsHost 它 在WPF窗口:新增 OnLoaded 处理 在WPF窗口:新增文本框定位下的的WindowsFormsHost Created WPF Window (starting point of app) Created WinForms Form with two TextBox on it WPF window: Added WindowsFormsHost to it WPF window: Added OnLoaded handler WPF window: Added Textbox positioned under the WindowsFormsHost

OnLoaded 处理我:

System.Windows.Forms.Form f = new WinFormsForm();
f.TopLevel = false;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.windowsFormsHost1.Child = f;

当我现在运行应用程序:

When I now run the application:

在没有被聚焦(OK) 在我点击第一个文本框的WindowsFormsHost ,它获得焦点(OK) 在我preSS选项卡上,重点将转到的WindowsFormsHost (OK) 在我再次preSS标签,焦点将返回到第一个文本框的WindowsFormsHost (没有确定;应该离开的WindowsFormsHost 并给予重点文本框在WPF窗口的底部) 在我点击了WPF的文本框(位于后在的WindowsFormsHost ),它获得焦点(OK) 在我preSS选项卡上,重点转到第1文本框在的WindowsFormsHost - 因为它应该结束后去开始。因此,这是确定的太 在我再次点击WPF文本框和preSS Shift + Tab,焦点转到第二个文本框的的WindowsFormsHost (OK) 在我preSS选项卡上,重点转到第1文本框在的WindowsFormsHost (去年初在WFH)(不会确定) Nothing is focussed (ok) I click on the first TextBox in the WindowsFormsHost, it gets focus (ok) I press tab, focus goes to 2nd TextBox in the WindowsFormsHost (ok) I press tab again, focus goes back to 1st TextBox in the WindowsFormsHost (not ok; should have left WindowsFormsHost and given focus to the textbox at the bottom of the WPF window) I click on the textbox in the wpf (placed after and under the WindowsFormsHost), it gets focus (ok) I press tab, focus goes to 1st textbox in WindowsFormsHost - as it should go to beginning after the end. So this is ok too I click the wpf textbox again and press shift+tab, focus goes to 2nd textbox in WindowsFormsHost (ok) I press tab, focus goes to 1st textbox in WindowsFormsHost (goes to beginning in WFH) (not ok)

我如何使聚焦的行为一样,如果我有一类只控制?这意味着在这种情况下,WFH-1日 - 文本框,WFH-第二,文本框,WPF的文本框的Tab键顺序。

How do I make the focus behave like if I had only controls of one type? Meaning a tab order of WFH-1st-Textbox, WFH-2nd-Textbox, WPF-Textbox in this case.

推荐答案

据我已经找到了文章,这似乎不可能完成的任务。据 MSDN博客条目(部分Hwnds)的Windows窗体控件一直都在WPF控件的层次结构中的顶级。该 MSDN文章(部分获取的WPF消息循环消息)指出,发生在一个的WindowsFormsHost事件WPF之前,将被处理的元素,甚至知道他们。

According to the articles I have found, that seems not possible to accomplish. According to a MSDN Blog Entry (section Hwnds) the Windows Forms controls always are on top of WPF controls in the hierarchy. The MSDN article (section Acquiring Messages from the WPF Message Loop) states that events occurring in a WindowsFormsHost element will be processed before WPF is even aware of them.

所以,我认为由pressing TAB键触发事件是由WindowsFormsHost元素(导致其他文本框的焦点)处理。在封闭的WPF窗口的事件将永远不会被碰到,因为它已处理。就当你preSS在WPF文本框TAB键的另一边,WPF处理这个事件的本身和控制链的正常处理。有了这个重点会在WindowsFormsHost元素,并从那里使用键盘,你不能离开它一个文本框。

So I assume that the event fired by pressing the TAB key is processed by the WindowsFormsHost element (resulting in the focus of the other textbox). In the enclosing WPF window the event will never be encountered because "it has already been processed". On the other side when you press the TAB key in the WPF textbox, WPF is handling the event itself and the control chain is processed normally. With this the focus will get to a textbox in the WindowsFormsHost element and from there you can't leave it using the keyboard.

我知道这不会帮助你的当前的问题,但我希望它解释了一些事情。

I know this will not help your current problem but I hope it explains some things.

补遗 如果你不依赖于使用表单控件,你可以改变它变成一个WinForms用户控件与它相同的控制元件。之后,你改变WindowsFormsHost元素的通过以下方式初始化

ADDENDUM If you are not dependent on using a form control, you could change it into a WinForms user-control with the same control elements in it. After that you change the initialization of the WindowsFormsHost element in the following way:

System.Windows.Forms.UserControl control = new WinFormUC();
windowsFormsHost1.Child = control;

类WinFormUC是包含提到的文本框我的WinForms用户控件。在我的测试TAB键的pressing集中在文本框接二连三不管它的一个WinForms或WPF文本框。

The class WinFormUC is my WinForms user-control containing the mentioned textboxes. In my test the pressing of the TAB key focused the textboxes one after another regardless whether its a Winforms or a WPF textbox.

 
精彩推荐
图片推荐