2

I'm want to conditionally prevent the Enter/Return key from selecting the highlighted item in a ComboBox drop down. So I wired up an event handler to the ComboBox.PreviewKeyDown so that I could set the Handled property, but the event handler is never entered. When I use Snoop to watch the events, the PreviewKeyDown event is fired for other keys but it never fires when I press the return key; not even at the Window level. Why isn't the event firing?

EDIT: The ComboBox needs to be editable (IsEditable=true). Then open the drop down list. Begin typing in an item in your list and it should select it for you. Press the return key.

xr280xr
  • 12,621
  • 7
  • 81
  • 125
  • 1
    I just tried this on a new project with a single TextBox and had no issues. Can you include some code? – Adam Barney Nov 03 '11 at 17:08
  • I tried this with a `ComboBox` which had a `PreviewKeyDown` event handler that simply wrote the key pressed to the debug window, and it worked fine. – Rachel Nov 03 '11 at 17:45
  • Sorry, I should've tried that first. Try the steps in my edit above. Actually, I may have just realized the problem right now. The actual drop down component of the ComboBox is a PopUp control right? It has the keyboard focus when it's visible and it probably has its own visual tree? What I'm trying to accomplish is if you type in text that doesn't exist in the list, I don't want Return to select anything. By default it selects the previous match. – xr280xr Nov 03 '11 at 18:29
  • Did you resolve this problem? I am also looking for answer. THnkas! – Jayson Ragasa Feb 27 '13 at 00:16
  • @JaysonRagasa Not really. I ended up deselecting it and closing the drop down in the KeyUp if it didn't match as a work around – xr280xr Feb 27 '13 at 17:30

2 Answers2

0

Try this

// prevent selecting an item when a comboboxitem is highlighted
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter || e.Key == System.Windows.Input.Key.Return)
    {
        e.Handled = true;
    }
    else
    {
        //if (base.IsDropDownOpen == false)
        //{
        //  base.IsDropDownOpen = true;
        //}
    }

    //base.OnPreviewKeyDown(e);
}
Jayson Ragasa
  • 1,011
  • 4
  • 19
  • 33
  • 3
    Would you consider adding some narrative to explain why this code works, and what makes it an answer to the question? This would be very helpful to the person asking the question, and anyone else who comes along. – Andrew Barber Feb 27 '13 at 00:56
0

That's because WPF internally handles the event when the drop-down is open and enter key is pressed. That's the default WPF behavior.

To solve, extend the ComboBox and override the OnPreviewKeyDown method.

public class MyComboBox : ComboBox
{
    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        bool isDropDownOpen = IsDropDownOpen;

        base.OnPreviewKeyDown(e);

        if (isDropDownOpen)
        {
            e.Handled = false;
        }
    }
}
CaesarMyMate
  • 98
  • 1
  • 6