简体   繁体   English

在Xamarin.Forms中的分组ListView中搜索

[英]Searching in grouped ListView in Xamarin.Forms

Background 背景

I followed this guide to create a grouped ListView in Xamarin.Forms . 我按照本指南Xamarin.Forms创建了一个分组的ListView Doing so, the following Property is given which is bound as the ItemSource of the ListView : 这样做,给出以下Property ,该Property被绑定为ListViewItemSource

DevicesGrouped = new ObservableCollection<Grouping<string, Device>>(sorted);

Everything works just fine, including the grouping functionality. 一切正常,包括分组功能。

To search (and thereby filter) the entries in the list, I have tried to implement this pattern : 要搜索(从而过滤)列表中的条目,我尝试实现此模式

this.ItemsSource = DevicesGrouped
        .Where (x => x.Name.ToLower ()
        .Contains (filter.ToLower ()));

The issue 问题

The problem is, that I do not have access to the properties of the Device as the objects reside in Groupings . 问题是,由于对象驻留在Groupings ,因此我无法访问Device的属性。 I can only access the Key of the DevicesGrouped which, in this case, is a Manufacturer , where I want to search for the Name . 我只能访问DevicesGroupedKey ,在这种情况下,它是一个Manufacturer ,我想搜索Name The problem in searching for the Key is also, that the ListView scrolls to the position of the Key and thus hides the elements in the ListView behind the grouping header. 搜索Key的问题还在于, ListView滚动到Key的位置,从而将ListView的元素隐藏在分组标题后面。

My question is thus, how would one access the properties of the ObservableCollection which is Grouped? 我的问题是,如何访问ObservableCollection的Grouped属性?

I have tried keeping a List which I use when I filter, but doing so, the app crashes, since Groupings are enabled on the ListView itself. 我已经尝试保留我在过滤时使用的List,但这样做,应用程序崩溃,因为ListView本身已启用Groupings

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

I found an answer to my question, and will post it if others run into the same issue. 我找到了我的问题的答案,如果其他人遇到同样的问题,我会发布。

Simple do the following when filtering your Grouped ListView: 过滤Grouped ListView时,请执行以下操作:

_groupedDeviceList.ItemsSource =
                DevicesGrouped.Where(o => o.Any(p => p.Name.ToLower().Contains(filter.ToLower())));

_groupedDeviceList is of course your own list. _groupedDeviceList当然是你自己的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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