wpf 如何从 CollectionViewSource 添加或删除项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11823341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add or remove an item from a CollectionViewSource?
提问by Gypsy
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?
我想将一个 datagrid.itemsource 绑定到一个匿名类型的 List,所以我将它绑定到一个 collectionViewSource,但是我需要在列表中添加或删除一个项目,但我不知道如何?
采纳答案by Eli Arbel
CollectionViewand CollectionViewSourceare, as their name suggest, only views. You can only manipulate the original collection. You should use an ObservableCollectionif you want the changes to be automatically reflected in your UI.
CollectionView并且CollectionViewSource,顾名思义,只是views。您只能操作原始集合。ObservableCollection如果您希望更改自动反映在您的 UI 中,您应该使用。
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.
我建议不要使用匿名类型进行数据绑定。但是如果你必须 - 他们实现了 Equals 方法,所以你应该能够使用具有相同属性的新项目或使用项目的索引来删除项目。

