如何添加或从CollectionViewSource删除一个项目? [英] How to add or remove an item from a CollectionViewSource?

查看:151
本文介绍了如何添加或从CollectionViewSource删除一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绑定一个datagrid.itemsource与匿名类型的列表,所以我把它绑定到一个collectionViewSource,但我需要添加或删除项目列表中,但我不知道怎么样?

I want to bind a datagrid.itemsource to a List with anonymous type,so i bind it to a collectionViewSource,but i need to add or remove an item to the list,but i don't know how?

推荐答案

的CollectionView Col​​lectionViewSource 是,他们的名字暗示,只有的意见。你只能操纵原始集合。您应该使用的ObservableCollection 如果您想更改自动反映在UI。

CollectionView and CollectionViewSource are, as their name suggest, only views. You can only manipulate the original collection. You should use an ObservableCollection if you want the changes to be automatically reflected in your UI.

我建议不要使用匿名类型数据绑定。但是如果你必须 - 他们实现Equals方法,所以你应该能够删除使用新的项目具有相同的属性或使用该项目的项目指数

I would advise against using anonymous types for data binding. But if you must - they implement the Equals method, so you should be able to remove items using new items with equal properties or using the item's index.

这篇关于如何添加或从CollectionViewSource删除一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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