问题描述
我正在尝试为 CalendarDatePicker 设置日期格式以用于全球化目的.我试过这个:
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;
但它给了我以下异常:
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)
我检查了我设置的值,对我来说它看起来很正常:"M/d/yyyy"
I checked the value I had set and it looked normal to me: "M/d/yyyy"
我做错了什么?
推荐答案
当您使用 CalendarDatePicker,它的 TextBox
部分的字符串内容由 DateTimeFormatter.
When you use a CalendarDatePicker, the string content of the TextBox
portion of it is created by a DateTimeFormatter.
"M/d/yyyy"
看似正常,但是
重要您不能随意组合组件并必须获得有效模板.唯一有效的模板是由提供的语法定义的模板.
如果你阅读参考DateTimeFormatter
类,你会发现没有像"M/d/yyyy"
这样的格式,对于这样的相同格式M/d/yyyy"
,我觉得应该是这样的:
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}";
这篇关于uwp CalendarDatePicker 设置日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!