views:

29

answers:

1

Hi Experts,

I have a combobox on a window in wpf and i am trying to capture the down arrow key of this combobox but i am not able to do so. The following is the only code i have for the combobox.

<ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"
              PreviewKeyDown="comboBox1_PreviewKeyDown" KeyDown="comboBox1_KeyDown" IsEditable="True"/>

C#

private void comboBox1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
            MessageBox.Show("hi");
    }

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
            MessageBox.Show("hi");
    }

The event is not even hit when i press down arrow key.

Please help! Thanks in advance!

Regards,

Samar

+1  A: 

Try handling PreviewKeyUp (or KeyUp) instead. If that does not work, then there must be more to your window or code (are you handling other instances of these events)?

Wonko the Sane
Hi Wonko the PreviewKeyUp event worked. But can you please explain y PreviewKeyDown event did not get fired for down arrow key???
samar
I am not positive on this, but I believe it has to do with the fact that PreviewKeyDown is a tunneling event. By making the ComboBox editable, I believe that the TextBox that becomes part of the ControlTemplate for the ComboBox is swallowing up that event.
Wonko the Sane