在WPF中,如何在使用PreviewKeyDown处理后取消按键?

时间:2022-09-30 19:44:22

In my WPF Datagrid I capture the "delete" key, process it, and then the datagrid itself deletes the row from the UI by continuing to process its own handler for the delete key (which is what I want).

在我的WPF Datagrid中,我捕获“删除”键,处理它,然后数据网格本身通过继续处理自己的删除键处理程序(这是我想要的)从UI中删除该行。

But now I want CTRL-S to open up a search bar, which it does, but it also goes on to blank out the cell the user was on when he pressed CTRL-S, so I am looking for a way to tell the datagrid to cancel the key press so that it is not executed on the Datagrid.

但是现在我想让CTRL-S打开一个搜索栏,但它也会在按下CTRL-S时将用户所在的单元格空白,所以我正在寻找一种方法来告诉datagrid取消按键,使其不在Datagrid上执行。

How can I cancel a keypress like this?

如何取消这样的按键?

XAML:

<toolkit:DataGrid x:Name="TheDataGrid" DockPanel.Dock="Bottom"
                  CanUserAddRows="False"
                  AlternatingRowBackground="#ddd"
                  CanUserSortColumns="true"
                  PreviewKeyDown="TheDataGrid_PreviewKeyDown"
                  AutoGenerateColumns="False"
                  RowEditEnding="TheDataGrid_RowEditEnding">

code-behind:

private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.S))
    {
        ShowSearchBar();
    }

    switch (e.Key)
    {
        case Key.Delete:
            DeleteCustomer(sender, e);
            break;
    }
}

2 个解决方案

#1


12  

e.Handled = true;

#2


1  

In a slightly different situation, I have a second argument of type PreviewKeyDownEventArgs, in which situation the solution is:

在稍微不同的情况下,我有一个类型为PreviewKeyDownEventArgs的第二个参数,在这种情况下解决方案是:

e.IsInputKey = true;

#1


12  

e.Handled = true;

#2


1  

In a slightly different situation, I have a second argument of type PreviewKeyDownEventArgs, in which situation the solution is:

在稍微不同的情况下,我有一个类型为PreviewKeyDownEventArgs的第二个参数,在这种情况下解决方案是:

e.IsInputKey = true;