How to open datepicker when tapping on an icon?

Sreejith Sreenivasan 1,001 Reputation points
2024-09-11T15:38:07.85+00:00

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.

I already saw this is a bug here. So any alternative solution for this?

Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Anonymous
    2024-09-12T07:11:58.5666667+00:00

    Hello,

    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.

      private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
        {
          var  handler = DA_DatePicker.Handler as IDatePickerHandler;
    #if ANDROID
            handler.PlatformView.PerformClick();
     
    #elif IOS || MACCATALYST
     
            DA_DatePicker.Focus();
     
    #endif
        }
    

    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 comments No comments

0 additional answers

Sort by: Most helpful

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.