none
ListView自身有没有筛选隐藏功能?怎样根据搜索关键词隐藏不符合筛选条件的项。 RRS feed

  • 问题

  • ListView自身有没有筛选隐藏功能?我想做个搜索,筛选数据,只是不想删除掉绑定集合里面的数据。仅仅让ListView隐藏不符合筛选条件的项,当然也得有显示全部项的方法,当然ListView的数据模板是确定的。也不希望在实体类中增加visibility类型的字段来实现。

    • 已编辑 Trian555 2018年8月14日 17:40
    2018年8月14日 17:39

答案

  • 您可以直接在数据源上做个过滤

    https://stackoverflow.com/questions/9615891/filter-an-observable-collection

    Bind your ListView directly to the filtered collection instead of the ObservableCollection by creating a property -

    public ICollectionView YourFilteredCollection
    {
       get
       {      
          var source = CollectionViewSource.GetDefaultView(collection.Collection);
          source.Filter = p => Filter((ProjectViewModel)p);
          return source;
       }
    }

    So, now simply you need to call Refresh() on your collection on your check boxes state changed event like this -

    YourFilteredCollection.Refresh();

    To refresh the collection based on any state change in the item class, you can generalize it by hooking the PropertyChanged event of your item class (for this your class need to implement INotifyPropertyChanged) and from there you can call refresh like this -

    foreach (YourClass item in collection.Collection)
    {
      item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
    }
    
    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      YourFilteredCollection.Refresh();
    }

    So, whenever any property changes in your item class, your collection will be filtered.


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    • 已标记为答案 Trian555 2018年8月15日 4:41
    2018年8月15日 0:08