C# – WPF TextBox doesn’t take input, space and backspace works

cwpf

I have textbox inside a usercontrol and I add the usercontrol to the MainWindow with the following XAML:

 <Views:MyUserControl />

I have one TextBox in MyUserControl, the problem is that the TextBox doesn't take any input. Backspace och space works, but if I press the letter och numbers no text is added to the TextBox.

I have made sure that the text is not just hidden in the TextBox.
I have also tried to add a RichTextBox to MyUserControl with the same result, it doens't take any input (beside space och backspace).

I have also tried to add a TextBox to the MainWindow with the same result; it doens't take any input (beside space och backspace).

Also MyUserControl is added in a TabControl and TabItem.

Any clues?

Edit: Additional information
Forgot to write that I'm opening/creating the WPF Window from a WinForm application.
When I set my startup project in VS10 to be my WPF-project it work great with the keyboard input to the TextBox.

How come?

Im opening/creating my WPF windows with the following code:

MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
mw.Show();

Edit: Solution
So I guess my real problem was that is was opening the WPf project from a WinForms application.

I added the following code:

MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();

"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms."
http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx

Best Answer

Answer to my own question (if someone else run into the same problem): If you open a WPF-form from a WinForms application you have to do the following to get keyboard input:

MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();

"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms." http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx

Related Topic