Aasish Grg
Aasish Grg

Reputation: 25

Use search command of search bar on list view with mvvm pattern

I'm a beginner on xamarin mvvm patter. Currently I'm trying to create a search bar that searches the word from a list of names. I tried to write some codes on comman function on my view model and bind it on the SearchCommand of search bar on view. But it didn't work. Here's my code

namespace HelloWorld.ViewModel
{

    public class CustViewModel : INotifyPropertyChanged
    {

        private custmodel _custmodel;


        public custmodel custmodel
        {
            get { return _custmodel; }
            set
            {
                _custmodel = value;
                NotifyPropertyChanged();
            }
        }

        private string _message;
        public string message
        {
            get { return _message; }
            set
            {
                _message = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<string> _list;
        public ObservableCollection<string> Items
        {
            get
            {
        return _list;
            }
            set
            {
                _list = value;
                NotifyPropertyChanged();
            }
        }

        public Command SaveCommand
        {
            get
            {
                return new Command(() =>
                {
                    message = "Your task : " + custmodel.name + ", " + custmodel.surname + " was successfully saved!";
                });
            }
        }

        private string _bar;

        public string Bar
        {
            get { return _bar; }
            set { _bar = value;
        }
    }

    public Command SearchCommand
{
    get
    {
            return new Command(() =>
            {
                string keyword = _bar;
                IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword));
                _list = new ObservableCollection<string>(searchresult);
                NotifyPropertyChanged();
            }
                );


    }
}

public CustViewModel()
{

    custmodel = new custmodel
    {
        name = "Aasish",
        surname = "Gurung",
        email = "[email protected]"
    };
    _list = new ObservableCollection<string>();
    _list.Add("Saurab");
    _list.Add("Basanta");
    _list.Add("Abhishek");
    _list.Add("Surace");
    _list.Add("Amir");

    }

    public event PropertyChangedEventHandler PropertyChanged;


    //public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

Here is my xaml file

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="HelloWorld.Styling"
         BackgroundColor="AntiqueWhite" Title="Hello"
         xmlns:converters="clr-namespace:HelloWorld.Converters; assembly=HelloWorld">

<StackLayout>
    <SearchBar x:Name="MainSearchBar" Text="{Binding Bar}"
               SearchCommand="{Binding SearchCommand}"/>
    <ListView ItemsSource="{Binding Items}"/>
</StackLayout>

Upvotes: 0

Views: 4548

Answers (1)

hvaughan3
hvaughan3

Reputation: 11105

First, make sure you are setting your ContentPage's BindingContext to your CustViewModel.

Also, you should stop assigning and adding things to _list and instead assign and add things to your public Items property. Items is the one that will trigger the NotifyPropertyChanged() method when it has been assigned to.

So change your SearchCommand to this:

return new Command(() => {
    string keyword = _bar;
    IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword));

    Items = new ObservableCollection<string>(searchresult);

    //NotifyPropertyChanged(); //There is no reason to trigger NotifyPropertyChanged on this command each time the getter is run, I would imagine that this could cause an infinite loop
});

Upvotes: 2

Related Questions