Filter ListView

Hello,

I am creating a list view on UI elements and populate it by file names. also getting the filter values on another textfield.

how can I filter this list view using this text value? I can repopulate the list view but it doesn’t seem right. also I can’t seem to filter the itemsSource of the listview

thanks

ListView doesn’t expose filtering on the itemsSource right now. What you can do is to update your itemsSource and call Refresh on the ListView. Virtualization should prevent this operation from becoming unscalable as you will only regenerate the visible items upon the call to Refresh.

This means it is up to you to provide a source items list that handles the filtering efficiently. For example, you can implement a virtual IList that runs your filtering logic on the real file list and only returns the items you accept through the IList interface.
Of course you have to correctly implement things like Count and the indexer to make sure you stay compatible with ListView’s virtualization as well.

@cemleme I have a repository for some UIElements controls, one of them uses a ListView to display a filtered set of info like @wbahnassi_unity suggests:

private void UpdateOptionList()
{
MatchedSuggestOption.Clear();
optionList.itemsSource = MatchedSuggestOption;
optionList.selectedIndex = -1;
if (string.IsNullOrEmpty(textEntry.text))
{
optionList.Refresh();
return;
}
MatchedSuggestOption.AddRange(SuggestOption.Where(matchingSuggestOptions));
optionList.Refresh();
}

This is sourced from here if you want to side some wider context.
I found this solution to have solid performance.

1 Like