Search code examples
datepickermaui

How to open datepicker when tapping on an icon?


I have an icon on the UI and when tapping on it I need to open the calendar directly.

<DatePicker 
    x:Name="DA_DatePicker"
    HorizontalOptions="CenterAndExpand"
    IsVisible="False"
    Date="{Binding SelectedDate, Mode=TwoWay}"
    MinimumDate="2024-01-01"
    MaximumDate="2025-12-31"
    Format="MM/dd"
    DateSelected="OnDateSelected"/>

Is there any way to do it on MAUI? When tap on it I set the visibility to true, but on UI only current date is showing.


Solution

  • For android, you can use handler.PlatformView.PerformClick(); to make the DatePicker appear when image was tabbed. For iOS DA_DatePicker.Focus(); will open the datepicker.

        public void CalendarClicked(object sender, EventArgs e)
        {
            if (DeviceInfo.Platform == DevicePlatform.iOS)
            {
                DA_DatePicker.Focus();
            }
            else
            {
                var handler = DA_DatePicker.Handler as IDatePickerHandler;
    #if ANDROID
                    handler.PlatformView.PerformClick();
    #endif
            }
        }