New to Telerik UI for .NET MAUI? Start a free 30-day trial

.NET MAUI DatePicker Commands

Telerik UI for .NET MAUI DatePicker exposes a number of commands for programmatic manipulation of its popup rendering.

DatePicker Commands

The DatePicker supports the following commands, which enable you to control the display of its popup and clear the selected date:

  • ToggleCommand(ICommand)—Allows you to show or hide the popup that is used for selecting a date value.
  • ClearCommand(ICommand)—Allows you to clear the displayed date.

The following example demonstrates how to set the ToggleCommand and ClearCommand.

1. Set ToggleCommand and ClearCommand.

<StackLayout>
    <Button Text="Toggle Command" Command="{Binding Source={x:Reference datePicker}, Path=ToggleCommand}"/>
    <Button Text="Clear Command" Command="{Binding Source={x:Reference datePicker}, Path=ClearCommand}"/>
    <telerik:RadDatePicker x:Name="datePicker" />
</StackLayout>

2. Add the following namespace:

xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"

OK and Cancel Buttons

Through the popup or the drop-down, users can pick a date. The date value must be confirmed or rejected with the OK or Cancel buttons located in the popup or drop-down.

The DatePicker allows you to add a custom logic for the Accept and Cancel commands which are executed when the OK or Cancel buttons are clicked.

  • AcceptCommand(ICommand)—Defines the command, which confirms the current selection of the picker and closes the popup or drop-down. Use AcceptCommandParameter to pass a parameter to the command execute method.
  • CancelCommand(ICommand)—Defines the command, which rejects the current selection of the picker and closes the popup or drop-down. Use CancelCommandParameter to pass a parameter to the command execute method.

You can apply the Accept and Cancel commands for the popup mode by setting the PopupSettings property of the DatePicker. For the drop-down mode, use the DropDownSettings property.

The example shows how to define the commands and command parameters in the PopupSettings. The same is valid for the DropDownSettings.

1. Define the control and add the commands:

<VerticalStackLayout>
    <telerik:RadDatePicker x:Name="datePicker" PickerMode="Popup"> 
        <telerik:RadDatePicker.PopupSettings>
            <telerik:PickerPopupSettings AcceptCommand="{Binding Accept}"
                                         AcceptCommandParameter="{Binding Date, Source={x:Reference datePicker}}"
                                         CancelCommand="{Binding Cancel}"
                                         CancelCommandParameter="{Binding Date, Source={x:Reference datePicker}}"/>
        </telerik:RadDatePicker.PopupSettings>
            <telerik:RadDatePicker.BindingContext>
                <local:ViewModel/>
            </telerik:RadDatePicker.BindingContext>
    </telerik:RadDatePicker>
</VerticalStackLayout>

2. Set the ViewModel:

public class ViewModel
{
    public ICommand Accept { get; set; }
    public ICommand Cancel { get; set; }

    public ViewModel()
    {
        this.Accept = new Command(this.OnAccept);
        this.Cancel = new Command(this.OnCancel);
    }

    private void OnAccept(object param)
    {
        Application.Current.MainPage.DisplayAlert("Date selected", String.Format("New Date: {0:d}", (DateTime)param), "OK");
        // implement your custom logic here
    }

    private void OnCancel(object param)
    {
        var message = param != null ? String.Format("Current date: {0:d}", (DateTime)param) : "Currently no date is selected";
        Application.Current.MainPage.DisplayAlert("Date Selection Canceled", message, "OK");
        // implement your custom logic here
    }
}

3. Add the following namespace:

xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"

See Also

In this article