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:
- Created WPF
Window
(starting point of app) - Created WinForms
Form
with twoTextBox
on it - WPF window: Added
WindowsFormsHost
to it - WPF window: Added
OnLoaded
handler - WPF window: Added
Textbox
positioned under theWindowsFormsHost
In the OnLoaded
handler I got:
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:
- Nothing is focussed (ok)
- I click on the first
TextBox
in theWindowsFormsHost
, it gets focus (ok) - I press tab, focus goes to 2nd
TextBox
in theWindowsFormsHost
(ok) - I press tab again, focus goes back to 1st
TextBox
in theWindowsFormsHost
(not ok; should have leftWindowsFormsHost
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)
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.