Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Filter a list view by keyword...

$
0
0

I have created a simple list with a keyword textentry field that filters the list based on the keyword. I can filter the list but unsure how i unfilter the list! ie When i filter the collection based on the keyword..i can't seem to refilter the original list as the original collection is filtered.

using etech.zspotter.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace etech.zspotter.ViewModels
{
public class ClientsViewModel : BindableBase
{
#region Fields

    public ObservableCollection<ListViewContactsInfo> contactsInfo;

    #endregion

    #region Constructor


    INavigationService _navigationService;
    public DelegateCommand<string> TextChangedCommand { get; set; }
    public DelegateCommand<object> SelectCommand { get; set; }
    public ClientsViewModel(INavigationService navigationService, IPageDialogService ipageserve, IEventAggregator ea)
    {
        _navigationService = navigationService;
        TextChangedCommand = new DelegateCommand<string>(TextChangedCommandHandler);
        SelectCommand = new DelegateCommand<object>(SelectCommandHandler);
        GenerateSource(100);
    }

    private void TextChangedCommandHandler(string obj)
    {
        keyword = obj;
    }

    private async void SelectCommandHandler(object obj)
    {
        await _navigationService.NavigateAsync($"Sites?site=33");
    }

    #endregion

    #region Properties

    string _keyword;
    public string keyword
    {
        get
        {
            return _keyword;
        }
        set
        {
            _keyword = value;
            if (string.IsNullOrEmpty(_keyword))
            {
                ContactsInfo.Clear();
                foreach (var item in contactsInfo)
                {
                    ContactsInfo.Add(item);
                }
            }
            else
            {
                var ContactsInfo = new ObservableCollection<ListViewContactsInfo>(this.contactsInfo.Where(x => x.ContactName.Contains(keyword.ToString())));
                ContactsInfo.Clear();
                foreach (var item in ContactsInfo)
                {
                    ContactsInfo.Add(item);
                }

            }
        }
    }

    public ObservableCollection<ListViewContactsInfo> ContactsInfo
    {
        get
        {
            return this.contactsInfo;
        }
        set { SetProperty(ref this.contactsInfo, value); }
    }

    #endregion

    #region ItemSource

    public void GenerateSource(int count)
    {
        ListViewContactsInfoRepository contactRepository = new ListViewContactsInfoRepository();
        contactsInfo = contactRepository.GetContactDetails(count);
    }

    #endregion



}

}


Viewing all articles
Browse latest Browse all 91519

Trending Articles