问题
On my View I have a AutoSuggestBox(searchfield) and ListView, my ListView's ItemSource is bounded to my VM Class property:
private Class1 _searchMatches;
public Class1 SearchMatches {
get { return _searchMatches; }
set { this.Set(ref _searchMatches, value); }
}
On My Class1 I have a LoadItems Task:
async Task> LoadItems()
var stocks = _response.products?
.Select(s => new MyClass(PLService.DtoToModel(s)))
.ToList();
var items = stocks.GroupBy(p => p.productModel.Description)
.Select(p => p.First())
.ToList();
return items;
When i type test on the AutoSuggestBox and hit enter, What is the simplest way to filter items where(item.description == searchterm)? just filter it and update itemsource, not rewriting the property
回答1:
You can use <SearchBox> and it's QuerySubmitted event. But it will work well with <TextBox> too.
If you need to refilter your Items - just create two lists, one to store your Items and another for items displaying.
Here is a <SearchBox> sample:
private List<MyClass> _items; // store for your items
private List<MyClass> _displayItems;
public List<MyClass> DisplayItems // list to show
{
get { return _displayItems; }
set { SetProperty(ref _displayItems, value); }
}
private void SearchBoxQuerySubmitted(SearchBoxQuerySubmittedEventArgs eventArgs)
{
searchTerm = eventArgs.QueryText?.Trim();
Filter(searchTerm);
}
private void Filter(string searchTerm)
{
if (_items == null)
return;
IQueryable<MyClass> items = _items.AsQueryable();
if (!string.IsNullOrEmpty(searchTerm))
{
searchTerm = searchTerm.ToLower();
items = items.Where(x => x.productModel.Description.ToLower().Contains(searchTerm));
}
DisplayItems = items.ToList();
}
来源:https://stackoverflow.com/questions/45363486/how-can-i-filter-list-property