问题
I'm trying to set a date format for the CalendarDatePicker for globalisation purposes. I tried this:
endDate = (FindName("cdpEnd") as CalendarDatePicker);
endDate.DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
endDate.Date = workObject.EndCal;
But it gave me the following exception:
System.ArgumentException: The parameter is incorrect.
formatTemplate
at Windows.UI.Xaml.Controls.CalendarDatePicker.put_DateFormat(String value)
at App1.PageWorkObject.rootGrid_Loaded(Object sender, RoutedEventArgs e)
I checked the value I had set and it looked normal to me: "M/d/yyyy"
What am I doing wrong?
回答1:
When you use a CalendarDatePicker, the string content of the TextBox
portion of it is created by a DateTimeFormatter.
"M/d/yyyy"
seems to be normal, but
Important You can't arbitrarily combine components and necessarily obtain a valid template. The only valid templates are those defined by the provided grammar.
If you read refer to the DateTimeFormatter
class, you will find there is no format like "M/d/yyyy"
, for the same format like "M/d/yyyy"
, I think it should be like this:
endDate.DateFormat = "{month.integer}/{day.integer}/{year.full}";
来源:https://stackoverflow.com/questions/35807360/uwp-calendardatepicker-set-date-format