简体   繁体   中英

Search and Filter data in ObservableCollection with ICollectionview

I use ICollectionView to search in the ObservableCollection The program works well and I can do the search operation

public ICollectionView ItemsView => CollectionViewSource.GetDefaultView(DataList);
ItemsView.Filter = o => Filter(o as PackageModel);

private bool Filter(PackageModel item)
        {
            return SearchText == null
                       || item.Name.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) != -1
                       || item.Publisher.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) != -1;

        }

Now I want to filter data in the datagrid For example, items with IsInstalled = true

public bool IsShowOnlyInstalledApps
        {
            get => _isShowOnlyInstalledApps;
            set
            {
                SetProperty(ref _isShowOnlyInstalledApps, value);
                if (value)
                {
                    var filter = new Predicate<object>(item => ((PackageModel)item).IsInstalled);
                    ItemsView.Filter = filter;
                }
                else
                {
                    ItemsView.Filter = null;
                }
            } 
        }

I can see that the items in the datagrid are filtered But the search operation no longer works

use this:

return SearchText == null
                       || (item.Name.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) != -1)
                       || (item.Publisher.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) != -1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM