开发者

How can I disable the F4 key from showing the items in a ComboBox

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 general开发者_如何学Goly 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?


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜