How can I create a custom Picker with grouped items?

Jorge Nuricumbo 60 Reputation points
2023-12-27T14:15:18.9833333+00:00

I'm looking to create a custom Picker that can group items between 'Recently Used' and 'All Items,' as shown in the attached image. Are there any ideas or examples on how to implement this custom Picker? Thanks in advance.

User's image

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,890 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2023-12-28T05:33:41.1766667+00:00

    Hello,

    If you want to custom the picker's popup layout, you can do it by Entry and a popup layout.

    No need to use handler and custom picker's popup window for every platforms.

    Here are steps.

    1. When the Entry get focus, it will popup a window like the Picker.
    <Entry Focused="Entry_Focused"/>    
    
    1. I set entry.IsReadOnly = true;, it will not pop up keyboard for android/ios platforms. Then popup a window. And popup will return listview selected item.

    Here is Entry's background code.

    private async void Entry_Focused(object sender, FocusEventArgs e)
       {
           var popup = new PopUpPicker();
           if (e.IsFocused)
           {
               Entry entry = sender as Entry;
               entry.IsReadOnly = true;
               entry.Unfocus();
               var result = await this.ShowPopupAsync(popup, CancellationToken.None);
               
               if (result is string stringResult)
               {
                  
                   entry.Text = (string)result;
                  
               }
               entry.IsReadOnly = false;
           }     
      }
    
    1. Here is my popup layout, you can custom it by your need.
    <?xml version="1.0" encoding="utf-8" ?>
    <toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
                 x:Class="MauiCustomPickerDemo.PopUpPicker"
                >
        <VerticalStackLayout>
            <Label
                TextColor="Gray"
                Text="Recently used"
                VerticalOptions="Start"
                HorizontalOptions="Start" />
            <Label
                 Text="English"
                 VerticalOptions="Start"
                 HorizontalOptions="Start"
                />
    
    
           <ListView Header="All languages" SelectionMode="Single" ItemSelected="ListView_ItemSelected">
                <ListView.ItemsSource>
                    <x:Array Type="{x:Type x:String}">
                        <x:String>mono</x:String>
                        <x:String>monodroid</x:String>
                        <x:String>monotouch</x:String>
                        <x:String>monorail</x:String>
                        <x:String>monodevelop</x:String>
                    </x:Array>
                </ListView.ItemsSource>
            </ListView>
           
        </VerticalStackLayout>
    </toolkit:Popup>
    
    1. After you selected an item in the listview, this popup window will be closed.
    public partial class PopUpPicker : Popup
    {
        public PopUpPicker()
        {
            InitializeComponent();
        }
        private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            CloseAsync(e.SelectedItem);   
        }
    }
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful