当按下Delete键时,DataGrid失去焦点 [英] DataGrid Looses Focus When Delete Key is Pressed

查看:131
本文介绍了当按下Delete键时,DataGrid失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行MVVM,其中将DataGrid绑定到ObservableCollection,并使用DeleteItemCommand挂钩到DataGrid.InputBindings,如下所示:

 < DataGrid.InputBindings> 
< KeyBinding Key =删除 Command = {Binding DeleteItemCommand} />
< /DataGrid.InputBindings>

当用户按下Delete键但网格失去焦点时,项目和行将被删除。您必须单击或制表到网格,以便在单击删除以删除另一行之前,它可以重新获得焦点(非常令人讨厌)。我尝试设置DataGrid.CanUserDeleteRows = False,但没有任何区别。



我用ListView替换了DataGrid,并且ListView保持了焦点。

p>

这是DataGrid的错误还是我做错了?和平与爱,和平与爱!如果基础集合是可编辑的,则网格默认情况下会删除项目(如果该集合专用于此目的,那没问题,否则可以添加中间集合...)。我避免了任何键绑定,而是像这样设置网格:

< DataGrid ItemsSource = {Binding InvoiceItems} IsReadOnly = False CanUserDeleteRows = True CanUserAddRows = False>

ItemsSource集合的类型为BidningCollection<>



在我的ViewModel(我的DataContext)中我为CollectionChanged事件添加了一个处理程序:

InvoiceItems.CollectionChanged + = InvoiceItemsCollectionChanged;



并像这样实现它:

  private void InvoiceItemsCollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
if (e.Action!= NotifyCollectionChangedAction.Remove)
返回;
foreach(e.OldItems中的var oldItem)
{
//进行其他必要的处理
}
}

那是因为您可能至少有两种从基础集合中删除项目的方法(带有Del键的键盘,一些按钮)以及其他一些方法删除项目时需要注意的事情。


I'm doing MVVM where a DataGrid is bound to an ObservableCollection with a DeleteItemCommand hooked up to the DataGrid.InputBindings as follows:

  <DataGrid.InputBindings>
      <KeyBinding Key="Delete" Command="{Binding DeleteItemCommand}" />
  </DataGrid.InputBindings>

The item and row are removed when the user hits the delete key but the grid looses focus. You have to click or tab to the grid so it regains focus before hitting Delete to remove another row (pretty freaking annoying). I tried setting the DataGrid.CanUserDeleteRows="False" but it doesn't make any difference.

I replaced the DataGrid with a ListView and the ListView retains focus.

Is this a bug with the DataGrid or am I doing something wrong? Peace and love, peace and love!

解决方案

I solved this by using the built in functionality of WPF DataGrid. The grid handles removing items by default if the underlying collection is editable (if the collection is dedicated to this purpose that's no problem, otherwise an intermediate collection can be added...). I avoided any key bindings and just set up the grid like this:
<DataGrid ItemsSource="{Binding InvoiceItems}" IsReadOnly="False" CanUserDeleteRows="True" CanUserAddRows="False">
The ItemsSource collection is of type BidningCollection<>

In my ViewModel (my DataContext) I add a handler for CollectionChanged event:
InvoiceItems.CollectionChanged += InvoiceItemsCollectionChanged;

And implement it like this:

private void InvoiceItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action != NotifyCollectionChangedAction.Remove)
            return;
        foreach (var oldItem in e.OldItems)
        {
            //do any other processing necessary
        }
    }

That's because you will probably be having at least two ways of removing an item from you underlying collection (keyboard with Del key, some button) and maybe some other things to take care of when an item is deleted.

这篇关于当按下Delete键时,DataGrid失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆