Forum Xamarin.Forms

Can't detect OK button press on DatePicker

FactoryOptimizrFactoryOptimizr USUniversity ✭✭

I want to populate a label with the value from the Xamarin Forms DatePicker control. In my scenario, there a distinct difference between when a date has been selected and NO date has yet been selected (a value vs. the absence of a value), so I can't simply use the DatePicker's built-in label. Instead, I'm hiding the DatePicker in XAML, an invoking its Focus event to launch the control's UI.

The DatePicker UI self-initializes with the current date. In cases where the current date is the "correct" selection, a user seeing the current date selected in the UI will simply tap "OK"/"Done" to accept the pre-selected value. But no event fires in this case: it seems that it's impossible to determine when the "OK"/"Done" button is tapped in the UI. The relevant DatePicker events are...

DateSelected - fires only when a date has CHANGED from the previous value...no change, no event (as is the case when the default value is accepted).
PropertyChanged - same behavior as DateSelected, but must explicitly filter for the "Date" property.

So it seems I can't intercept the default selection in code without some sort of workaround.

I could respond to the Unfocused event, simply grabbing the Date property when ever Unfocused fires. However, if a date has already been selected when the UI is displayed and the user clicks "Cancel" (or taps outside the UI) to dismiss the date picker without changing the existing date, the likelihood is high of changing an existing date value that the user had NO intention of changing.

Another solution would be to respond to the Unfocused event as above, but setting a class-level variable to respond only the FIRST time the Unfocused event is fired (if it fires before the DateSelected event). On that first time I could grab the Date from the DisplayPicker (it's already available as the default value), then only respond to the DateSelected event thereafter to capture date changes. This is a better solution, but doesn't allow the user to cancel out of the date picker UI the first time it's launched without getting a value (explicitly or otherwise). And as I said before, in my scenario, there a distinct difference between when a date has been selected and NO date has yet been selected.

Has anyone found a fool-proof solution to this problem?

Are the Xamarin folks aware it's a problem???

If the Xamarin folks are lurking hereabouts, because the DatePicker UI is initialized with the current date, the DateSelected event should ALWAYS fire whenever the "OK"/"Done" button is tapped.

Answers

  • RVxRVx USMember ✭✭

    I may missunderstand what you need but....

    When you initializes the datepicker set the date to display, you could fire an event manually
    or
    have a private DateTime variable in the Page that you set the date to when the page is initialized and change that when DateSelected i fired.

  • FactoryOptimizrFactoryOptimizr USUniversity ✭✭

    In my scenario, it's important that the date value is blank until the user explicitly selects a date. That's why I'm using a label rather than simply the display of the DatePicker itself.

  • numineronuminero NLMember

    @FactoryOptimizr
    Your post is already from some months ago but did you happen to find a nice way to achieve your goal?
    I have exactly the same need and I find it incredible that it does not work that way.
    Thank you for your input!

  • FactoryOptimizrFactoryOptimizr USUniversity ✭✭

    @numinero, seems there's no fool-proof, elegant solution for this, so I went with "option B" above: I subscribe to the Unfocused event and set a class-level variable to respond only the FIRST time the Unfocused event is fired. That allows me to grab the default date as the "selected" date the first time the date picker appears, but also means that there's no true "cancel" the first time the date picker appears. It seems the "least bad" solution to me...

    Indeed, because I wanted to be able to format the date picker's display text (font, attributes, color, size), I wrapped my own user control that gives me exactly what I want. I'd be happy to share the code if you're interested.

  • numineronuminero NLMember

    @FactoryOptimizr, thank you for your (very) quick answer.

    I already have a Custom Renderer for the DatePicker to allow the Nullable value and I did play a while with this but failed to achieve the optimal solution. Currently when the DatePicker pops up I set the current date (default date) immediately making it valid even if the user presses Cancel. If today is what the user wants then it's already set otherwise another date would be selected and normal events will be called. As you said, the first time there is no true Cancel. However, next to the DatePicker I have a button to clear the date, so it is easy to start over.

    I appreciate your sharing of code (here or privately, as you prefer), to see if I can merge it with mine in some positive way. What you described with the Unfocused event is something I didn't find, so I don't know either what I can expect. I am still finding my way in Xamarin.Forms.

    Have a nice weekend!

  • MeccieMeccie USMember ✭✭
    edited January 2019

    not sure if you're looking for something like this but...this might help others out
    the event will only fire when a date has been selected.
    (i know it's an old post but some people might find this useful)

       DatePickerPlanGerecht.DateSelected += DateSelected;
    
    
        public void DateSelected(object sender, EventArgs e)
        {
            Console.WriteLine(DatePickerPlanGerecht.Date);
        }
    
  • FactoryOptimizrFactoryOptimizr USUniversity ✭✭

    Unfortunately, the DateSelected event doesn't fire if the date hasn't been explicitly changed.

    A user can tap the picker's OK button to accept the existing/default date, but the app would never know because the DateSelected event never fires.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @FactoryOptimizr said:
    Unfortunately, the DateSelected event doesn't fire if the date hasn't been explicitly changed.

    A user can tap the picker's OK button to accept the existing/default date, but the app would never know because the DateSelected event never fires.

    I think the DatePicker defaults to today's if you don't set the Date property and that is shown in the label on collapsed calendar. So you already have the DatePicker.Date set. I don't think you need to detect the OK button at all.

  • FactoryOptimizrFactoryOptimizr USUniversity ✭✭

    That's correct, however, as I mentioned in my original post, in my scenario there a distinct difference between when a date has been selected and NO date has yet been selected (a value vs. the absence of a value). I want the user to be presented with a BLANK field as a visual queue that they need to select a date. I want to avoid users ignoring to _**select **_ a date simply because a _default_ date is already present.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @FactoryOptimizr said:
    That's correct, however, as I mentioned in my original post, in my scenario there a distinct difference between when a date has been selected and NO date has yet been selected (a value vs. the absence of a value). I want the user to be presented with a BLANK field as a visual queue that they need to select a date. I want to avoid users ignoring to _**select **_ a date simply because a _default_ date is already present.

    I think you will have issues implementing this logic. In iOS, the DatePicker.Date changes as you roll the calendar wheels, even before you hit the Done button. In Android and UWP there are both OK, Cancel buttons.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    What I've done for this scenario is create a custom DatePicker that has a nullable DateTime. This required creating a whole new view and renderers, but heavily borrowed from Xamarin's source code.

  • AndyAndy Member ✭✭✭

    Please implement OK and CANCEL events for the date and time picker.
    I need to know if the user presses ok or cancel when I display the date and time picker to the user. The user should not have to change the value to trigger OK event.

  • abombabomb Member

    Agree, need these events

Sign In or Register to comment.