简体   繁体   中英

ListView / List Filter Windows Phone 8.1 C#

I have a list of data

public class PopImage
{
    public async Task<List<PopImage>> PopDatas()
    {
        string imgfolder = "PopularImages";
        var data = new List<PopImage>();
        StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        StorageFolder subfolder = await folder.GetFolderAsync(imgfolder);

        var files = await subfolder.GetFilesAsync();
        foreach (var items in files)
        {
            data.Add(new PopImage(imgfolder+"/"+items.DisplayName+ ".jpg", items.DisplayName));
        }

        return data;
    }

    public PopImage(string imagePath, string imageName)
    {
        ImagePath = imagePath;
        ImageName = imageName;
    }

    public string ImagePath { get; set; }
    public string ImageName { get; set; }
}

I want to add a textbox and filter it if textbox textchanged, what do I need to apply it?

You need to add a TextChanged event to your TextBox. First in your XAML add this:

<TextBox Name="tbListFilter" TextChanged="tbListFilter_TextChanged"/>

Then the code behind is:

private void tbListFilter_TextChanged(object sender, TextChangedEventArgs e)
{
     yourFilteredList = yourPopImageList.Where(p => p.ImageName.ToUpper().Contains(tbListFilter.Text.ToUpper())).ToList();
}

Based on @WPMed

I try to make a new list from filtered items

var FilteredList= new List<PopImage>();
        foreach (var data in popimagelist)
        {
            if(data.ImageName.ToUpper().Contains(FilterText.Text.ToUpper()))FilteredList.Add(data);
        }

Thank you for the help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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