Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBox doesn't take input, space and backspace works

Tags:

c#

wpf

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

like image 441
Felix Avatar asked Nov 12 '10 10:11

Felix


3 Answers

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

like image 137
Felix Avatar answered Nov 15 '22 10:11

Felix


maybe your user control is getting the keyboard event instead of your textbox? try to search in this way, it happens with mouse buttons.

like image 31
kite Avatar answered Nov 15 '22 10:11

kite


I had the same problem but my Environment was built such that I could not apply the solution above. As it turned out that wasn't the problem. It was much more easier than expected.

I host a WPF User Control inside WinForms. My WinForms MainView (Startup form) did override the "ProcessCmdKey". Because of logical errors it returned nothing.

Nevertheless I received all key events in code behind in my XAML file but this events never updated the "Text" of my text box. Only space and backwards did so and this were the key events I didn't receive in code behind.

So in case you don't think of the simplest solution here it is for VB.NET.

STRG+F "ProcessCmdKey" and make sure that you Return the correct value of you may completely uncomment it just to verify that it isn't causing you the same trouble as it did to me.

like image 1
Matthis Kohli Avatar answered Nov 15 '22 08:11

Matthis Kohli