How to set SelectedItem in Xamarin picker when page opens

MarcinJMarcinJ USMember ✭✭

I have a small project using XamarinForms and Prism MVVM. On the settings page I save Author's ID from the Picker. When I return to the settings page I want that Author to be selected by default in the picker.

This is my Picker in Xaml:

   <Picker x:Name="authorPicker" Title="Select Author" FontSize="Medium"
            HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
            ItemsSource="{Binding NoteAuthors}"
            ItemDisplayBinding="{Binding Name}"
            SelectedItem="{Binding SelectedAuthor, Mode=TwoWay}"
            Grid.Row="0" Grid.Column="1" />

When Author is selected I got this in ViewModel and it works fine:

private NoteAuthor _selectedAuthor;
public NoteAuthor SelectedAuthor
{
    get { return _selectedAuthor; }
    set
    {   if (_selectedAuthor != value)
        {
            SetProperty(ref _selectedAuthor, value);
        }
    }
}

In the ViewModel > OnNavigatingTo function I call the GetAuthor function which returns Author based on previously saved ID.

public async void GetAuthor(int author_id)
{
    NewNoteAuthor = await App.Database.GetAuthorById(author_id);
    if(NewNoteAuthor != null && NewNoteAuthor.ID > 0)
    {
        SelectedAuthor = NewNoteAuthor;
    }
}

How can I "jump" to this Author on Picker when page opens? The assignment in GetAuthor function doesn't work for me.

Best Answer

  • MarcinJMarcinJ US ✭✭
    Accepted Answer

    FYI, I have a solution from another forum:

    NoteAuthors = await // read them from db ...
    SelectedAuthor = NoteAuthors.SingleOrDefault(a => a.Id == author_id);

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited July 2018

    Having the property set should do it. You don't need to do anything.
    Make sure the binding mode is set to two-way.

    oh... You do. Not sure about Prism. What you have there I do a dozen times with regular out-of-the-box MVVM binding

  • Gigex42Gigex42 USMember ✭✭✭✭

    Yes had the same problem.

    Somehow the picker does not update when you bind it with an object. From UI to code it works though.
    My properties are filled correctly and the propertychanged event gets called. Still nothing shows on my label.

    I did try it then with not an observablecollection but an observablecollection and it works.. It Displays the text.

    I then just bound the title of the picker to SelectedAuthor.Name or what ever and it worked..

    Maybe a bug?

  • MarcinJMarcinJ USMember ✭✭
    Accepted Answer

    FYI, I have a solution from another forum:

    NoteAuthors = await // read them from db ...
    SelectedAuthor = NoteAuthors.SingleOrDefault(a => a.Id == author_id);

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I did try it then with not an observablecollection but an observablecollection and it works.

    That makes no sense. It doesn't with with an ObservableCollection but it works with an ObservableCollection. They are the same thing.

    Somehow the picker does not update when you bind it with an object. From UI to code it works though.

    "From UI to code it works" - no idea what you're trying to convey there. All typical binding is from UI to a property in code.

  • Gigex42Gigex42 USMember ✭✭✭✭

    @ClintStLaurent said:

    I did try it then with not an observablecollection but an observablecollection and it works.

    That makes no sense. It doesn't with with an ObservableCollection but it works with an ObservableCollection. They are the same thing.

    Somehow the picker does not update when you bind it with an object. From UI to code it works though.

    "From UI to code it works" - no idea what you're trying to convey there. All typical binding is from UI to a property in code.

    Okay my text got cut off.. Wanted to write it works with an ObservableCollection of string but not with ObservableCollection of myobject.

    What I meant with the binding is that when the user makes changes on the ui it works fine. But when I try to change the value of the property it does not change the ui. This works though when I use the ObservableCollection

    I know this is normal binding but somehow it does not work with a picker here.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited July 2018

    @Gigex42 said:

    @ClintStLaurent said:

    I did try it then with not an observablecollection but an observablecollection and it works.

    That makes no sense. It doesn't with with an ObservableCollection but it works with an ObservableCollection. They are the same thing.

    Somehow the picker does not update when you bind it with an object. From UI to code it works though.

    "From UI to code it works" - no idea what you're trying to convey there. All typical binding is from UI to a property in code.

    Okay my text got cut off.. Wanted to write it works with an ObservableCollection of string but not with ObservableCollection of myobject.

    What I meant with the binding is that when the user makes changes on the ui it works fine. But when I try to change the value of the property it does not change the ui. This works though when I use the ObservableCollection

    I know this is normal binding but somehow it does not work with a picker here.

    You need to stop putting in XML tags in your text without marking it as code... That's what's causing the site parser to eat your messages.

    You need to use Markdown to mark code like that as code."
    https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    ObservableCollection<string> displays correctly when tagged correctly

  • I'm having the same problem, can someone point me a solution?
    I have this picker:
    <Picker StyleClass="FieldSelect" Title="Selecione uma casa" ItemsSource="{Binding Casas}" SelectedItem="{Binding Casa, Mode=TwoWay}" ItemDisplayBinding="{Binding Descricao}" />
    And it works fine, but if I set the value on the ViewModel:
    Casa = App.conexao.Table<Models.Manutencao.Casa>().Where(i => i.Id == pedido.IdCasa).FirstOrDefault();
    It just doesn't change the View, I have two pickers in this form with this issue, the other inputs work fine.

Sign In or Register to comment.