如何过滤列表属性 [英] How can I filter List property

查看:104
本文介绍了如何过滤列表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图上,我有一个AutoSuggestBox(searchfield)和ListView,我的ListView的ItemSource绑定到了我的VM Class属性:

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

在我的Class1上,我有一个LoadItems任务:

On My Class1 I have a LoadItems Task:

异步任务> LoadItems()

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;

当我在AutoSuggestBox上键入 test 并按Enter键时,最简单的方法是在哪里过滤项目(item.description == searchterm)? 只需对其进行过滤并更新itemsource,而不重写该属性

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

推荐答案

您可以使用<SearchBox>及其QuerySubmitted事件.但是它也可以很好地与<TextBox>一起使用. 如果您需要重新过滤您的Items-只需创建两个列表,一个列表用于存储Items,另一个用于显示项目.

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.

这是一个<SearchBox>样本:

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

这篇关于如何过滤列表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆