WPF Events in Winforms

I have a Winforms application that uses a WPF (Avalon Edit, if important) control inside ElementHost.

It seems to work fine, but I would like to be able to handle the KeyPress events of this control in the Winforms way (without RoutedCommands and InputGestures), so although I could just handle the Form KeyDown event using the KeyPreview set, but the WPF events don't seem to go beyond form.

So, how can you access the KeyDown event in a WPF control in the form of Winforms?

+7
c # event-bubbling events winforms wpf
source share
1 answer

You can try adding your own event handler for WpfControl itself, instead of trying to connect to WinForm KeyDown.

Here is an example. Let's say your WinForm is of type Form1 , WpfControl is UserControl1 , and the caller for WpfControl is called (never guesses)) is elementHost.

 public Form1() { InitializeComponent(); elementHost.ChildChanged += ElementHost_ChildChanged; } private void ElementHost_ChildChanged(object sender, ChildChangedEventArgs e) { var ctr = (elementHost.Child as UserControl1); if (ctr == null) return; ctr.KeyDown += ctr_KeyDown; } void ctr_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { /* your custom handling for key-presses */ } 

UPD: e.KeyboardDevice.Modifiers (e is System.Windows.Input.KeyEventArgs ) stores information about Ctrl, Alt, etc.

+6
source share

All Articles