Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to set SelectedItem in Xamarin picker when page opens

$
0
0

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.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>