Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 42 ยท Controls and Binding

Input Controls and Pickers

Collect choices in .NET MAUI with Picker, DatePicker, TimePicker, CheckBox, RadioButton, SearchBar and Switch: key properties, events, selected values and a runnable selection-logic stand-in.

Text boxes are only one way to get information from a user. When the answer comes from a fixed set of options, a date or a yes/no choice, a dedicated control is faster for the user and safer for you, because the control cannot produce an invalid value. This lesson covers the choice controls of .NET MAUI: Picker, DatePicker, TimePicker, CheckBox, RadioButton, SearchBar and a short comparison of Switch with CheckBox.

The real MAUI controls cannot run on this site, so the XAML is read-only. The runnable blocks are labelled C# stand-ins that model the rules behind the controls (selection indexes, date ranges, radio groups). Those rules are the part that causes bugs in real apps.

Picker

A Picker shows a single selected item and opens a platform dialog (a dropdown, a wheel or a sheet) when tapped. You give it Title text, which is shown while nothing is selected, and either an ItemsSource or an Items list.

XML
<Picker x:Name="sizePicker"
        Title="Choose a size"
        SelectedIndexChanged="OnSizeChanged">
    <Picker.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>Small</x:String>
            <x:String>Medium</x:String>
            <x:String>Large</x:String>
        </x:Array>
    </Picker.ItemsSource>
</Picker>
C#
void OnSizeChanged(object? sender, EventArgs e)
{
    if (sizePicker.SelectedIndex == -1)
    {
        return;               // nothing selected
    }

    string size = (string)sizePicker.SelectedItem;
    DisplayLabel.Text = $"You picked {size}";
}

Three properties describe the selection: SelectedIndex (an int, -1 means nothing), SelectedItem (an object?) and, when the items are objects, ItemDisplayBinding tells the picker which property to show as text. With data binding you normally bind SelectedItem two-way to a property of your view model, which you will see in Data Binding Basics.

XML
<Picker Title="Country"
        ItemsSource="{Binding Countries}"
        ItemDisplayBinding="{Binding Name}"
        SelectedItem="{Binding SelectedCountry}" />

Two rules to remember. First, a Picker cannot be empty after the user picks something unless you set SelectedIndex = -1 in code. Second, if you replace the ItemsSource, the selection is reset, so restore it afterwards.

DatePicker and TimePicker

DatePicker has Date (a DateTime), MinimumDate, MaximumDate and Format (a .NET format string such as "D" or "dd MMM yyyy"). Its event is DateSelected, with OldDate and NewDate in the event arguments. TimePicker has Time (a TimeSpan), Format and the property-changed notification for Time (there is no dedicated event; you watch PropertyChanged or bind to it).

XML
<DatePicker MinimumDate="2025-01-01"
            MaximumDate="2030-12-31"
            Format="dd MMM yyyy"
            DateSelected="OnDateSelected" />

<TimePicker Time="09:30:00" Format="HH:mm" />

A common mistake is to treat Date as a date-only value. It is a DateTime, so the time part is midnight, but if you add TimePicker.Time to it you must do so deliberately: datePicker.Date + timePicker.Time. Another is forgetting that DatePicker limits are inclusive.

CheckBox, Switch and RadioButton

CheckBox is a box that is checked or unchecked: IsChecked (a bool), Color and the event CheckedChanged. Switch is a toggle with IsToggled and the event Toggled. Use a switch for settings that take effect at once ("Notifications on"), and a check box for items in a form that you confirm later ("I accept the terms").

XML
<HorizontalStackLayout Spacing="8">
    <CheckBox x:Name="terms" CheckedChanged="OnTermsChanged" />
    <Label Text="I accept the terms" VerticalOptions="Center" />
</HorizontalStackLayout>

<Switch IsToggled="{Binding NotificationsOn}" />

RadioButton lets the user choose exactly one option from a group. Radio buttons that share the same parent are one group automatically; to group radio buttons that sit in different containers, give them the same GroupName. Every radio button has IsChecked, Value (the object it represents) and Content. A group raises CheckedChanged on the button that gets checked.

XML
<VerticalStackLayout RadioButtonGroup.GroupName="delivery"
                     RadioButtonGroup.SelectedValue="{Binding DeliveryOption}">
    <RadioButton Content="Standard" Value="standard" />
    <RadioButton Content="Express" Value="express" />
    <RadioButton Content="Pick up in store" Value="pickup" />
</VerticalStackLayout>

Binding RadioButtonGroup.SelectedValue is the cleanest way to read the choice: it holds the Value of the checked button, so your view model does not need one boolean per option.

SearchBar is an Entry with a search icon and a search button. It has Text, Placeholder, SearchCommand, SearchCommandParameter and the events TextChanged and SearchButtonPressed. Use TextChanged for filter-as-you-type, and SearchButtonPressed when the search is expensive, for example a network call.

XML
<SearchBar Placeholder="Search products"
           SearchCommand="{Binding SearchCommand}"
           SearchCommandParameter="{Binding Text, Source={RelativeSource Self}}" />

A good filter-as-you-type handler waits for a short pause before it searches, so that a fast typist does not trigger a request per key press. You will build that debounce idea in the REST lesson.

Which control for which choice?

Pick the right control

Picker One of many fixed items
DatePicker / TimePicker A date or a time of day
RadioButton One of a few, all visible
CheckBox / Switch Yes or no

Fewer than five options? Radio buttons show them all at once.

1 / 2

A selection-logic stand-in

The next block is plain C# and models the rules of a Picker (an index of -1 means nothing is selected, replacing the items resets the selection) and a radio group (exactly one value, or none). It is a stand-in, not MAUI code.

C# Stand-in: Picker and RadioButton group rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The date rules are worth a second stand-in. The real DatePicker clamps the date into MinimumDate..MaximumDate, and this small program shows the arithmetic you can reuse when you validate dates yourself.

C# Stand-in: DatePicker range clamping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Common mistakes

  • Reading SelectedItem without checking for null when the user has not chosen anything yet.
  • Using SelectedIndexChanged for SelectedItem-based logic with a refreshed ItemsSource: the event fires with -1 while the list is replaced.
  • Putting radio buttons in different parents and expecting them to be one group (use GroupName).
  • Forgetting that TimePicker.Time is a TimeSpan, not a DateTime.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Picker.SelectedIndex return when the user has not chosen anything?

    The index is an int, and -1 is the value that means no selection. SelectedItem is null in that case.
  2. 2. How do you make RadioButtons in two different containers act as one group?

    Buttons with the same parent form a group automatically; GroupName joins buttons that have different parents.
  3. 3. Which type is TimePicker.Time?

    Time is a TimeSpan, the time elapsed since midnight. Add it to the DatePicker.Date to get a full date and time.
  4. 4. When is a Switch usually a better choice than a CheckBox?

    A switch communicates an immediate on/off setting; a check box is normally a form field confirmed with a button.

Summary

  • Picker selects one item; read SelectedIndex (-1 = none) or SelectedItem, and bind it in MVVM apps.
  • DatePicker and TimePicker give a DateTime and a TimeSpan; limits come from MinimumDate and MaximumDate.
  • CheckBox and Switch are boolean controls; RadioButton groups by parent or GroupName.
  • SearchBar offers TextChanged for live filtering and SearchButtonPressed for explicit searches.
  • The stand-ins on this page model the selection rules; the real controls render natively on each platform.

Next: Data Binding Basics