views:

112

answers:

2

You might not know this, but pressing the F4 key on a ComboBox makes it's drop-down item list appear. I believe this is the default behavior on Windows.

Does anyone know how to override this behavior in WPF (C#)?

I know that overriding default behavior is generally seen as bad practice, however in this case I have a rugged-device that runs XP Embedded. It has a handful of prominent Function keys (F1-F6) that need to trigger different events. This works fine, however when focused over a ComboBox the events aren't triggered as the ComboBox drops down.

I have tried catching the KeyDown event both on the form and on the ComboBox and listening for the F4 key, however it doesn't get this far as the key press must be handled at a lower level.

Any ideas? Thanks.

A: 

You could make your own ComboBox class and just inherit from the old one.. you should hopefully be able to override the keydown/up methods.

Jonas B
Tried this, unfortunately it didn't work.
Alex
+1  A: 

I'm not positive about XP Embedded, but on regular ol' XP, this works. Use PreviewKeyDown, and set e.Handled to true:

public MyWindowOrControl()
{
    InitializeComponent();
    cboTest.PreviewKeyDown += new KeyEventHandler(cboTest_PreviewKeyDown);
}

void cboTest_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F4)
        e.Handled = true;
}
Wonko the Sane
Excellent, using PreviewKeyDown instead of KeyDown works.Some information on the difference between the two:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f05377aa-8894-4b51-a87e-2e2771f20cd6http://blogs.msdn.com/jfoscoding/archive/2006/01/26/518181.aspx
Alex