Picker not showing value

marciosouzajuniormarciosouzajunior BRMember ✭✭
edited July 28 in Xamarin.Forms

I need to set a value to picker but it seems to not work in two way:

<Picker ItemsSource="{Binding myModelList}" 
        SelectedItem="{Binding myModel, Mode=TwoWay}" />

ViewModel:

public class TestViewModel : ObservableProperty
{
    public List<MyModel> myModelList { get; set; }
    private List<MyModel> _myModel;
    public List<MyModel> myModel;
    {
        get
        {
            return _myModel;
        }
        set
        {
            _myModel = value;
            OnPropertyChanged("myModel");
        }
    }

    async public Task prepareModels()
    {
        myModelList = await DB.query<MyModel>("SELECT * FROM MyModel;");
    }

    async public Task loadModel(int modelId)
    {
        myModel= await DB.getById<MyModel>(modelId);
    }

}

After call load model not is showing in picker. I created a button to show the selectedItem and its have an item selected, but got nothing in UI.
Any help?

Tagged:

Answers

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    you should be using BindablePicker instead of Picker and ObservableCollection instead of List

  • marciosouzajuniormarciosouzajunior BRMember ✭✭

    @JulienRosen said:
    you should be using BindablePicker instead of Picker and ObservableCollection instead of List

    The actual Picker is not bindable? What about this?

    I followed the on the link above and this example too, but they use a string list.

  • marciosouzajuniormarciosouzajunior BRMember ✭✭

    After play with MonkeyApp figure out that I cant use an "equal" object to set selected item like this:

    Monkey m = new Monkey
    {
        Name = "Squirrel Monkey",
        Location = "Central & South America",
        // etc
    };
    
    SelectedMonkey = m;
    

    The monkey seems to be selected but shows nothing on picker:

    I could make it works setting by the currently list:

    Monkey m = MonkeyData.Monkeys[3];

    So in my case I will need to find the object in list:

    myModel= myModelList.Find(e => e.id == modelId);

  • Did you solve this Marcio?

  • marciosouzajuniormarciosouzajunior BRMember ✭✭

    @BrettODonnell.3772 said:
    Did you solve this Marcio?

    Yes Brett, solved by setting this way:

    myModel = myModelList.Find(e => e.id == modelId);

Sign In or Register to comment.