Recover item from a Picker

5

I'm having a project in Xamarin Forms and want to retrieve a selected item from a Picker.

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

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

    
asked by anonymous 15.06.2018 / 15:33

1 answer

2

In this answer , whose question has a bit more detail about the implementation in question, you can see an example of how to use this feature.

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

As you did not share the code, I also do not know how to give a specific answer, but it would look something like this:

In 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 XAML of the page, make Binding with equivalent property:

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

I hope I have helped.

    
15.06.2018 / 16:01