Search code examples
c#xamarin.formspicker

OnChangeListener on Picker in Xamarin.Forms


Am having two pickers and i got each SelectedItem from the Pickers but they are not Firing my Method whenever i change there Values Why ?

Below is my Code :

This how i declared the selected Indices of Each Picker:

int selectedIndexYear = year_pk.SelectedIndex;
int selectedIndexTerm = term_pk.SelectedIndex;

Here is the logic behind of firing whenever i change the picker , but it's not working :

if (selectedIndexYear != -1 && selectedIndexTerm != -1)
{
    int term = Int32.Parse(term_pk.SelectedItem.ToString());
    //Below  is my method am Firing Each change of the Picker.
    OnResultsList(year_pk.SelectedItem.ToString(), term);
}

Solution

  • According to that scenario , i see you want to fire an Event whenever there's a change of an Item selected on a picker.

    I recommend you to use SelectedIndexChanged Event as below :

    year_pk.SelectedIndexChanged += delegate 
    {
        int term = Int32.Parse(term_pk.SelectedItem.ToString());
        //Below  is my method am Firing Each change of the Picker.
        OnResultsList(year_pk.SelectedItem.ToString(), term);
    }
    
    term_pk.SelectedIndexChanged += delegate 
    {
        int term = Int32.Parse(term_pk.SelectedItem.ToString());
        //Below  is my method am Firing Each change of the Picker.
        OnResultsList(year_pk.SelectedItem.ToString(), term);
    }