How to add Picker data in Xamarin Forms

木幡 弘文 1 Reputation point
2023-07-28T02:46:55.74+00:00

Is it possible to add additional data to a Picker that already contains data?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-07-28T03:13:13.26+00:00

    Hello,

    You can do this by adding itemsource in the background code.

    For example, you have Picker in the layout and add button for testing.

    <Button Text="click" Clicked="Button_Clicked" ></Button>
    <Picker x:Name="MyPicker"></Picker>
    

    Then you can set itemsource in the background code and add additional data to a Picker that already contains data.

    I use ObservableCollection to contains data, when you add data in it, it will update at the runtime.

    public partial class MainPage : ContentPage
        {
            ObservableCollection<string> strings;
           public MainPage()
            {
                InitializeComponent();
                strings = new ObservableCollection<string>();
                for (int i = 0; i < 10; i++)
                {
                    strings.Add(i.ToString());
                }
                MyPicker.ItemsSource = strings;
            }
            private void Button_Clicked(object sender, EventArgs e)
            {
                strings.Add("1234");
            }
        }
    

    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 comments No comments

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.