简体   繁体   English

单击“删除”键按钮删除DataGrid行(WPF)

[英]Delete DataGrid row (WPF) by clicking Delete key button

I have WPF 4 desktop-based application. 我有基于桌面的WPF 4应用程序。 In one of the windows of this application, I have DataGrid with data, bonded with SQL Server database (via ADO.NET Entity Framework). 在这个应用程序的一个窗口中,我有DataGrid数据,与SQL Server数据库绑定(通过ADO.NET实体框架)。 In order to manipulate data, I have a delete button, that deletes selected row from DataGrid and call SaveChanges() method. 为了操作数据,我有一个删除按钮,它从DataGrid中删除所选行并调用SaveChanges()方法。

Now I want to add support for keyboard manipulations, eg I want to let the user remove the row by selecting and clicking on Delete keyboard button. 现在我想添加对键盘操作的支持,例如我想让用户通过选择并单击Delete keyboard按钮来删除该行。

If I set CanUserDeleteRows="True" in window XAML, it removes selected row but doesn't make commit to database, in other words, it doesn't call SaveChanges() method. 如果我在窗口XAML中设置CanUserDeleteRows="True" ,它会删除所选行,但不会提交数据库,换句话说,它不会调用SaveChanges()方法。

I tried to add keyDown event handler to DataGrid a check if (e.Key == Key.Delete) , so run remove method that removes selected row and call SaveChanges() method, but it doesn't work. 我试图将keyDown事件处理程序添加到DataGrid检查if (e.Key == Key.Delete) ,因此运行remove方法删除所选行并调用SaveChanges()方法,但它不起作用。

My question is how can I add keyboard event handler to DataGrid that will let remove selected row and call SaveChanges() method or just run my own method, that deals with row removing from DataGrid and make commit to DB. 我的问题是如何向DataGrid添加键盘事件处理程序,它将删除所选行并调用SaveChanges()方法或只运行我自己的方法,该方法处理从DataGrid删除行并提交到DB。

Of course, if you have any other idea, related to my question, feel free to suggest. 当然,如果您对我的问题有任何其他想法,请随时提出建议。

Have you tried with the PreviewKeyDown event? 您是否尝试过使用PreviewKeyDown事件? Something like this 像这样的东西

<DataGrid x:Name="dataGrid" PreviewKeyDown="dataGrid_PreviewKeyDown">

private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
        var dataGrid = (DataGrid)sender;
        // dataGrid.SelectedItems will be deleted...
        //...
    }
}

Same as Ben's but all one has to do is enable the property CanUserDeleteRows by setting it to true and the delete button will have delete active. 与Ben的相同,但所有人必须做的是通过将属性CanUserDeleteRows设置为true来启用属性,并且删除按钮将激活删除。

As shown below in XAML for the DataGrid : 如下面在DataGrid XAML所示:

CanUserDeleteRows="True"

Or you could use the CommandManager, which only deletes the row if the row is selected (if a cell is editing, it backs up). 或者您可以使用CommandManager,如果选择了行,则只删除该行(如果单元格正在编辑,则会备份)。

Put this in your Window where the Datagrid is located. 将它放在Datagrid所在的Window中。

CommandManager.RegisterClassInputBinding(typeof(DataGrid),
                new InputBinding(DataGrid.DeleteCommand, new KeyGesture(Key.Delete)));

I see you managed to go ahead, but maybe this will be useful for others seing this post in search results. 我看到你成功了,但也许这对其他人在搜索结果中发表这篇文章会很有用。 You need to override the OnCanExecuteDelete method of the DataGrid, like: 您需要覆盖DataGrid的OnCanExecuteDelete方法,如:

public class MyDataGrid : DataGrid
{
    protected override void OnCanExecuteDelete(CanExecuteRoutedEventArgs e)
    {
        foreach(DataRowView _row in this.SelectedItems) //assuming the grid is multiselect
        {
            //do some actions with the data that will be deleted
        }
        e.CanExecute = true; //tell the grid data can be deleted
    }
}

But this is just for manipulating pure graphics. 但这仅仅是为了操纵纯图形。 For saving to database or other actions use the data source of your data grid. 要保存到数据库或其他操作,请使用数据网格的数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM