could not capture down arrow in combobox in wpf

2019-02-28 10:34发布

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.

1条回答
叛逆
2楼-- · 2019-02-28 11:05

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)?

查看更多
登录 后发表回答