Hi!
Anyone knows why calling Focus() on a Forms DatePicker opens the picker on Android and IOS, but not on Windows (WPF)?
I've seen some posts on bugzilla discussing about this issue, but they are almost all 2-3 years old and none of them seems to be correct.
Posts
Whether Xamarin did it this way intentionally or not, I don't know. However, on devices being used without a touch-screen interface, the user would expect to be able to tab around the controls on the screen using the keyboard without controls automatically expanding. Tabbing onto a Picker, DatePicker or TimePicker, should put the focus onto the control, with the current value displayed, but without automatically expanding the control.
@JohnHardman Thanks for your reply!
Well it makes sense.. Do you know if there's a workaround or another way to open Pickers without clicking directly on them ? I need to open it on a button click event.
I haven't done it (I don't need it), but I believe Windows.UI.Xaml.Controls.Button.Flyout.ShowAt is the method you'd be after. You'd need to create a custom DatePickerRenderer for UWP, in which you find the appropriate Windows.UI.Xaml.Controls.Button by iterating through the descendents of the Control's parent, looking for the "FlyoutButton".
So, something based on:
GetDescendantsByName would be implemented using Windows.UI.Xaml.Media.VisualTreeHelper
Hope that helps. I know a number of people have asked for this functionality since UWP support was added to XF. If you get it working, it would be a useful piece of code to share :-)
@JohnHardman
I'll definetly try this and share it if it works.
Thanks !
Finally, i looked into it today and found a solution.
Turns out that the WPF DatePicker has a property named IsDropDownOpen that once set to true opens the calendar.
I made a renderer for the picker and overridden the OnElementPropertyChanged method to detect when the Forms Picker IsFocus property changed to then show the calendar. Unfortunately, the focus property doesn't seem to change at all.
I ended up making a custom style based on the base style of the WPF DatePicker control using Blend. In this style, I only changed the visibility of the DatePickerTextBox and the Button to Hidden. Then, when I click the button to show the picker (the one in my layout, not the one inside the WPF Picker), I set the Forms Picker IsVisible property to true and catch that in the OnElementPropertyChanged of the renderer where I set the IsDropDownOpen property of the WPF Picker to true. Finally, I added a handler for the SelectedDateChanged event of the WPF Picker to change the Forms Picker date when user clicks on the calendar.
here's my renderer :
XamDatePicker is an alias for the Xamarin.Forms.DatePicker
InvisibleDatePicker is a custom control I made to apply the custom style (see below)
`
using XamDatePicker = Xamarin.Forms.DatePicker;
`
And here's my custom DatePicker style (InvisibleDatePicker) :
***** Check the bottom part to find the Button and DatePickerTextBox ******
`
`
Again, thanks for your help! @JohnHardman
Glad you got it working. I must have been in need of coffee when I responded about UWP instead of WPF :-)
Just to clarify for readers, the above solution works for WPF DatePicker (System.Windows.Controls.DatePicker) but not for UWP Datepicker (Windows.UI.XAML.Controls.DatePicker), as the UWP DatePicker does not have an exposed IsDropDownOpen property.
I'm looking into finding a solution for UWP DatePicker. My current approach is to try to trigger a space keystroke programmatically once focus is received. Still looking for a mechanism to do that in my UWP custom renderer.