Handling events from user control containing a WPF textbox
- by Tom
I've been successful in putting a WPF textbox into an existing windows form using ElementHost and a user control. It looks like the following:
Public Class MyUserControl
Private _elementHost as ElementHost = New ElementHost
Private _wpfTextbox as System.Windows.Controls.Textbox = New System.Windows.Controls.Textbox
Public Sub New()
Me.Controls.Add(_elementHost)
_elementHost.Dock = DockStyle.Fill
_elementHost.Child = _wpfTextbox
_wpfTextbox.SpellCheck.IsEnabled = True
End Sub
End Class
Now, when I put this user control onto a windows form, it accepts keyboard input and the form itself can get text from the textbox. However, when I try to handle keypress (or keydown, keyup) events from the user control, nothing happens.
I've looked into this problem and did a bit of research but now I'm a little confused as to what I really need to be looking at. The following are the topics that I am somewhat confused about.
HwndSource
Relaxed delegates
WPF routed events
If I am interpreting everything correctly, input from WPF textboxes are handled differently compared to regular form textboxes. It seems like both aren't compatible with each other but there must be some way to "pass" keys from one to another?
I'd appreciate any help.