views:

180

answers:

1

I'm working in a WPF project, which basically is a class library project that implements the MVVM pattern. For clarity purposes I'm gonna say that I just have a single window with a single textbox control on it.

Now, I'm adding this dll to another project, which is a Windows Forms project, and I'm calling the window with the textbox control from this project.

In my Windows Forms project I have a single window with a button on it that calls the WPF window when I click it, and also, before showing the WPF window, it makes the WPF window its child and then shows it.

This is the code that I'm using for calling my WPF window:


MyWPFWindow wpfWin = new MyWPFWindow ();
WindowInteropHelper helper = new WindowInteropHelper(wpfWin);
helper.Owner = this.Handle;
wpfWin.Show();

This code works good and it shows the WPF window, however the problem is that when I try to enter text in the textbox control I can't. The "delete" and "backspace" keys do work, and the curious thing is that if I use ShowDialog() instead of Show() then everything works just fine, but I can't use ShowDialog() because I need to have access to the parent window.

Can anyone help me figure it out why this is happen.

Thanks!

+1  A: 

You need to call ElementHost::EnableModelessKeyboardInterop passing in your WPF window instance. This installs a message filter in the WinForms message loop which forwards all the input to the WPF window when it's active.

So here's what your final code should look like:

MyWPFWindow wpfWin = new MyWPFWindow ();

WindowInteropHelper helper = new WindowInteropHelper(wpfWin);
helper.Owner = this.Handle;

ElementHost.EnableModelessKeyboardInterop(wpfWindow);

wpfWin.Show();
Drew Marsh
it worked perfect! many thanks!
Vic