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.