How to overwrite ctrl + a select all from list box?

时间:2016-04-04 16:44:38

标签: c# .net wpf

I was hoping that this code below would overwrite it, since I am assigning new stuff. But instead it executes both, selecting all and my message box

private void EventSetter_OnHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    {
        System.Windows.MessageBox.Show("ctrl a");
    }
}

please help thanks

1 个答案:

答案 0 :(得分:1)

If you handle the PreviewKeyDown event for the ListBox, you should be able to mark the event as handled, and the Ctrl+A should be ignored:

private void OnListBoxKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    {
        e.Handled = true;
    }
}