0
votes

I've simplified my problem to the code below which can simply be dropped into LinqPad and run.

The problem is when I tab through the WPF controls hosted inside the WinForms MDI child - the focus moves between textboxes until the last at which point focus jumps to the second MDI child (which in my example is all WinForms and tabbing between the textboxes works fine there.)

How can I prevent the WPF tabbing swapping from one MDI child to another and instead keep the focus in the WPF textboxes. (If WinForms controls appear before/after the ElementHost then ideally the focus would travel from WinForms to WPF to WinForms then back to the first WinForms.)

var mdiParent = new Form();
mdiParent.IsMdiContainer = true;
mdiParent.Width = 800;
mdiParent.Height = 600;
mdiParent.Show();

var otherChild = new Form();
otherChild.MdiParent = mdiParent;
var flow = new FlowLayoutPanel();
flow.Controls.Add(new TextBox());
flow.Controls.Add(new TextBox());
flow.Controls.Add(new TextBox());
otherChild.Controls.Add(flow);
otherChild.Show();

var form = new Form();
form.MdiParent = mdiParent;

var elementHost = new ElementHost();
elementHost.Dock = DockStyle.Fill;
form.Controls.Add(elementHost);

string xaml = @"
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
    <TextBox />
    <TextBox />
    <TextBox />
</StackPanel>
";

elementHost.Child = (UIElement)System.Windows.Markup.XamlReader.Load(new XmlTextReader(new StringReader(xaml)));

form.Show();
1

1 Answers

0
votes

This isn't necessarily an answer to the original question but I did implement a workaround in my application. By creating a WinForm Button and adding it as the last control in the Forms controls collection the focus is sent from WPF to this control and then back to the first control on the Form correctly. I've positioned the control off screen but the 'solution' is a little ugly. As I type this I'm also wondering whether I can set the TabStop on this control to false and it's mere existence is enough for the WinForms/WPF focus code to move the focus correctly?