How to enable typing and search functionality inside a Picker in .NET MAUI?

Bhuwan 801 Reputation points
2025-03-02T07:27:56.81+00:00

How to enable typing and search functionality inside a Picker in .NET MAUI?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,969 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 35,921 Reputation points Microsoft External Staff
    2025-03-03T06:58:22.8366667+00:00

    Hello,

    You could use the Picker and SearchBar on the page, and custom the layout.

    For example:

    On the Page

    <VerticalStackLayout>
    <SearchBar Placeholder="Search items..." SearchButtonPressed="SearchBar_SearchButtonPressed"  />
    <Picker x:Name="MyPicker"
      Title="Select a monkey" ItemsSource="{Binding MyList}">
    </Picker>
    </VerticalStackLayout>
    

    Collection

    public ObservableCollection<string> MyList { get; set; } = new ObservableCollection<string>
    {
        "Baboon",
        "Capuchin Monkey",
         "Blue Monkey",
         "Squirrel Monkey",
         "Golden Lion Tamarin",
         "Howler Monkey",
         "Japanese Macaque"
     
    };
    public ObservableCollection<string> Results { get; set; } = new ObservableCollection<string> {  };
    

    Match the result when press to search

    private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
    {
     
         SearchBar searchBar = (SearchBar)sender;
     
         foreach (string item in MyList)
         {
             if (item.Equals(searchBar.Text))
             {
                 Results.Add(item);
             }
         }
         if (Results.Count > 0)
         {
             MyPicker.ItemsSource = Results;
     
             MyPicker.SelectedItem = Results[0];//you could type Baboon for testing
         }
         else
         {
             MyPicker.ItemsSource = MyList;
         }
    }
    

    Best Regards,

    Wenyan Zhang


    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.