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);