我如何更新一个ObservableCollection类单个项目? [英] How do I update a single item in an ObservableCollection class?

查看:460
本文介绍了我如何更新一个ObservableCollection类单个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ObservableCollection类如何更新一个单一的项目?

How do I update a single item in an ObservableCollection class?

我知道该怎么做了添加。我知道如何在for循环搜索的ObservableCollection一个项目在一个时间(使用次数作为项目的大写金额的再presentation),但我怎么恰克现有项目。如果我做了的foreach,找到该项目需要更新,如何我把回的ObservableCollection>

I know how to do an Add. And I know how to search the ObservableCollection one item at a time in a "for" loop (using Count as a representation of the amout of items) but how do I chage an existing item. If I do a "foreach" and find which item needs updating, how to I put that back into the ObservableCollection>

推荐答案

您不必删除项,变更,再加入。你可以简单地使用LINQ FirstOrDefault 的方法来找到所需的产品使用适当的predicate和改变它的属性,如:

You don't need to remove item, change, then add. You can simply use LINQ FirstOrDefault method to find necessary item using appropriate predicate and change it properties, e.g.:

var item = list.FirstOrDefault(i => i.Name == "John");
if (item != null)
{
    item.LastName = "Smith";
}

删除或添加项目的ObservableCollection 将产生 Col​​lectionChanged 事件。

这篇关于我如何更新一个ObservableCollection类单个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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