uwp CalendarDatePicker set date format

怎甘沉沦 提交于 2019-12-13 00:57:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!