通过 ICollectionView 过滤 ObservableCollection 并使用它

Filter ObservableCollection by ICollectionView and use it

我希望能够根据用户 filtering criteria 过滤我的 ObservableCollection。目前我有 datagridviewvirtual mode:

中使用了这个集合
private ObservableCollection<WarehouseReadModel> _warehouse = new ObservableCollection<WarehouseReadModel>();

有什么方法可以代替ICollectionView吗?例如,当程序启动时,它显示来自 _warehouse 的所有数据,但是当完成来自 GUI 的任何过滤时,以某种方式将其分配给 ICollectionView 或什至从开始而不是分配给 ICollectionView 没有任何过滤器和然后在用户需要时应用过滤器?如果可能,请提供工作示例。

您可以有两个 ObservableCollection 属性(或字段):

public ObservableCollection<WarehouseReadModel> AllWareHouses { get; set}
public ObservableCollection<WarehouseReadModel> DisplayWareHouses {get; set}

一开始,您将加载第一个属性“AllWareHouses”中的所有数据。 当用户选择任何过滤器时,您将使用 LINQ 查询来过滤“AllWareHouses”。

查询结果将分配给“DisplayWareHouses”。您将使用此 属性 绑定到您的 DataGrid。

DisplayWareHouses = AllWareHourse.Where(...)