WPF Events in Winforms

I have an Winforms application that is using a WPF control (Avalon Edit if it matters) inside an ElementHost.

It seems to be working fine, but I would like to be able to handle KeyPress events of this control in the Winforms manner (without RoutedCommands and InputGestures), so I though I could just handle the Form's KeyDown event with KeyPreview set, but WPF events don't seem to bubble up to the Form.

So basically, how can you access a KeyDown event on a WPF control in the Winforms manner?

Answers


You can try to add custom event handler for WpfControl itself, instead of trying to connect to WinForm's KeyDown.

Here's example. Let's say: your WinForm is of type Form1, WpfControl is UserControl1, and element host for WpfControl is called (won't ever guess)) - 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 info about Ctrl, Alt, etc.


Need Your Help

Adobe CQ5's sidekick is empty (no components shown)

jsp content-management-system cq5

I've added a paragraph system component in my template, trying to emulate the Geometrixx's contentPage. Everything is fine but I can't drag any components into the system because there are none to ...

JUnit test for console input and output

java junit io console user-input

I have only one method main. How to check System.out.println() and replace Scanner to input values automatically using JUnit?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.