CollectionViewSource 筛选器很慢
本文关键字:筛选 CollectionViewSource | 更新日期: 2023-09-27 18:34:19
当列表很大时,过滤会变慢。有没有更快的方法来处理过滤器?目前这就是我所拥有的。
CollectionViewSource orderListCVS;
private void showCompletedRB(object sender, RoutedEventArgs e)
{
CollectionView coll = orderListCVS.View as CollectionView;
coll.Filter = null;
orderListCVS.Filter += new FilterEventHandler(showCompletedFilter);
}
private void showCompletedFilter(object sender, FilterEventArgs e)
{
Order order = e.Item as Order;
if (order != null)
{
e.Accepted = (order.Status == Data.Status.Done);
}
}
你有没有试过这种方式:
public partial class MainWindow : Window
{
private ICollectionView orderListCVS;
private List<Order> orders;
public MainWindow()
{
InitializeComponent();
orders = new List<Order>();
orderListCVS = CollectionViewSource.GetDefaultView(orders);
}
private bool showCompletedFilter(object sender)
{
Order order = sender as Order;
if (order != null)
{
return order.Status == Data.Status.Done;
}
return false;
}
private void showCompletedRB(object sender, RoutedEventArgs e)
{
orderListCVS.Filter = showCompletedFilter;
}
}
这是使用 collectionView 的最常见方式。