Retrieve item from a picker

Asked

Viewed 254 times

5

I have a project in Xamarin Forms and I want to recover a selected item from a Picker.

Picker is being fed by a list of Objects in its "Itemsource" property and I am showing the values of that list with the "Itemdisplaybindind" property".

What I want now is to send the selected item in the Picker there to my Viewmodel, can anyone tell me how to do this?

1 answer

2


In that reply, whose question has a little more detail about the implementation in question, you can see an example of how to use this resource.

Basically, you can use the property SelectedItem of Picker to make Binding with your View Model.

Since you didn’t share the code, I also don’t know how to give a specific answer, but it would be something like this:

In the view model create the property representing the selected item:

public class Minhaviewmodel : Inotifypropertychanged { ...

private Objeto objetoSelecionado;
public Objeto ObjetoSelecionado 
{ 
    get { return objetoSelecionado; } 
    set 
    {
        if(objetoSelecionado != value)
        {
            objetoSelecionado = value;
            OnPropertyChanged("ObjetoSelecionado");
        }  
    }
}

...

}

In the page XAML, do the Binding with the equivalent property:

<Picker SelectedItem="{Binding ObjetoSelecionado}"
        ItemsSource="{Binding ListaObjetos}"
        ...
        />

I hope I’ve helped.

  • 1

    Hello Diego, thank you for answering. It worked, I managed to get the selected value in my Viewmodel, I thank you too.

  • @Q.Wesley Good, glad I can help. Good development!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.