-->

How to add or remove an item from a CollectionView

2020-04-12 04:14发布

问题:

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?

回答1:

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.

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.