Picker not showing value

marciosouzajuniormarciosouzajunior BRMember ✭✭
edited July 2017 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:

Best Answer

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 ✭✭
    Accepted Answer

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

    Yes Brett, solved by setting this way:

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

  • JassimRahmaJassimRahma USMember ✭✭✭

    I have another problem..

    In the attached you can see that when selecting the value from the picker it doesn't show it as selected although it was already selected because the second picker data is changed base on the first..

    For example, the first is the country and the second is the area, when changing the country it will change the value and it will update the pickerArea but it will not show the selected country on the pickerCountry.

    I have noticed this problem in Android Emulator but iOS Simulator has no issue..

    Here is my XAML:

    <Picker x:Name="pickerSignupCountry" SelectedIndexChanged="pickerSignupCountry_SelectedIndexChanged" Title="البلد" Margin="0,30,0,10" ItemDisplayBinding="{Binding country_name}" SelectedItem="{Binding country_code_alpha2}" SelectedIndex="{Binding country_code_alpha2}" />

    How to fix this please?

Sign In or Register to comment.