DataGrid行删除 - 寻找最佳实践,最简洁的方法来实现它 [英] DataGrid row removal - Looking for the best practices, cleanest way to implement it

查看:113
本文介绍了DataGrid行删除 - 寻找最佳实践,最简洁的方法来实现它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上研究过这个话题,找到了多种方法。有些帖子是十年+旧,所以我不确定这些建议是否是最新的。我自己的代码(由过去的几个程序员处理)包含一个很好的响应式实现
但是读取该代码是一场噩梦。

I have researched this topic on the Internet, found multiple approaches. Some posts are a decade+ old, so I am not sure whether those suggestions are current. My own code (handled by several programmers in the past) contains a nice, responsive implementation but reading that code is a nightmare.

我的简单初始绑定是一个List<模型取代。我能够删除底层模型中的项目。显然,PreviewKeyDown是要捕获的最佳事件:

My simplistic initial binding was to a List<Model>. I was able to remove items in the underlying model. Apparently, the PreviewKeyDown is the best event to catch:

private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
        foreach (ItemBasics row in dataGrid.SelectedItems)
        {
            ListOfBasicInfo.Remove(row);
        }
    }
}

问题是GUI DataGrid没有更新。接下来,我尝试从List<>升级到ObservableCollection<> ;.这带来了"收藏品已被修改;枚举操作可能不会执行"。似乎
,Observable和Enumerable之间存在冲突。我找到了用不同线程处理这种冲突的代码。我还看到了代码,它创建了该集合的临时副本。

The problem was that the GUI DataGrid was not updated. Next, I tried upgrading from a List<> to an ObservableCollection<>. This brings issues of "The collection has been modified; the enumeration operation may not execute". It seems that there is a conflict between being Observable and being Enumerable. I found code that treats this conflict with different threads. I have also seen code that makes a temporary copy of the collection.

嗯,你得到了它的要点。我不知所措,害怕走上一条过时,低效的道路。

Well, you get the gist of it. I am at a loss, afraid to embark myself on an obsolete, inefficient path.

TIA

推荐答案

我不确定以下内容是否适用,但感觉非常相似。我在浏览列表时删除项目时遇到问题。正确的方法是反过来。

I am not sure whether the following applies, but it feels very similar. I had trouble deleting items while traversing a list. The proper way to do it is traversing in reverse.

-T


这篇关于DataGrid行删除 - 寻找最佳实践,最简洁的方法来实现它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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