11
votes

A WPF form that I launch from a WinForms window showed up with all textboxes as uneditable when launched as a modeless dialog. I used ElementHost.EnableModelessKeyboardInterop to tackle the issue & it worked there.

I am also opening the same WPF UI from an MS Excel as an add-in. The EnableModelessKeyboardInterop hack doesn't work there. Whenever I try to edit my WPF textbox, the focus shifts to Excel and the keyboard input is rendered on Excel instead of on my WPF textbox. Any ideas on how to fix this?

P.S. - This is in continuation to my earlier question on SO: WPF modeless dialog renders textbox uneditable

1

1 Answers

11
votes

Solved it, courtesy of this link: Running WPF Application with Multiple UI Threads

         var thread = new Thread(() =>
            {
                var wpfWindow = new WPFWindow();
                wpfWindow.Show();
                wpfWindow.Closed += (sender2, e2) => wpfWindow.Dispatcher.InvokeShutdown();

                Dispatcher.Run();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();