Maui picker for Android selecteditem issue

Haviv Elbsz 2,111 Reputation points
2024-01-04T19:51:48.2566667+00:00

Ver 17.9 preview 2 android Ver 34 Hello how

I can change the text of a selecteditem.

               <x: string> item 1<x: string>

               <x: string> item 2<x: string>

               <x: string> item 3<x: string>  

  I want programmatically change   'item 3' to something else. 

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

Answer accepted by question author
  1. Anonymous
    2024-01-05T03:03:51.2+00:00

    Hello,

    You can do this by adding the picker's ItemSource to a System.Collections.ObjectModel.ObservableCollection

    For example, you have a Picker like following code.

     <Picker x:Name="picker"
      Title="Select a monkey" >
    

    Then, you can create a new ObservableCollection, add data in it and set the picker's ItemSource.

     ObservableCollection<string> values = new ObservableCollection<string>();
         public MainPage()
         {
             InitializeComponent();
             for (int i = 0;i<5;i++)
             {
                 values.Add(i.ToString());   
             }
             picker.ItemsSource = values;
         }
    

    I used a Button click event to make a test, if you want to change the third one's value in the Picker. You can set it directly. this value in the DataPicker will be changed.

    private async void OnCounterClicked(object sender, EventArgs e)
    {
         values[2] = "test";}
    }
    

    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.


0 additional answers

Sort by: Most helpful

Your answer

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