Selected Item Binding is not working for the Picker.

Vaibhav Methuku 165 Reputation points
2025-03-18T04:46:30.7366667+00:00

Hello, I had picker, I want to explicitly bind the selected item. I mean before the user selection iteself i want to display an item in the picker.

here is my picker.

 <Picker

     x:Name="languageSelectionPicker"

     BackgroundColor="White"

     FontFamily="AppRobotoRegularFont"

     ItemDisplayBinding="{Binding LanguageName}"

     ItemsSource="{Binding SupportedLanguages}"

     SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}" />  

When ever im navigating to that page im doing like this

    vm.SelectedLanguage = App.GetSupportedLanguages().FirstOrDefault(e => e.LanguageKey == Thread.CurrentThread.CurrentCulture.Name);

But this is not working as expected.

Thanks
Vaibhav Methuku.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-03-18T05:42:36.6266667+00:00

    Hello,

    Firstly, you need to set the SelectedLanguage type to be consistent with the Model.

    For example, you have a Model like following code.

    public class MyModel
    {
        public string LanguageName { get; set; }
    }
    

    Then you need to set type of the SelectedLanguage to the MyModel like following code.

    internal class MyViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "") =>
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
     
    
        private MyModel selectedLanguage;
     
        public MyModel SelectedLanguage
        {
            get { return selectedLanguage; }
            set { selectedLanguage = value;OnPropertyChanged(); }
        }
    
    
        public ObservableCollection<MyModel> SupportedLanguages { get; set; }
     
        public MyViewModel()
        {
            SupportedLanguages=new ObservableCollection<MyModel>();
    
     //add static value for testing.
            for (int i = 0; i < 10; i++)
            {
                SupportedLanguages.Add( new MyModel() { LanguageName="test"+i });
            }
        }
    }
    

    For example, I want to set the SelectedLanguage that MyModel's LanguageName is "test1", then return back MyModel to the SelectedLanguage.

    vm.SelectedLanguage = vm.SupportedLanguages.Where<MyModel>(t=>t.LanguageName.Equals("test1")).FirstOrDefault();
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.