我正在玩弄新的Windows Store通用应用程序模板,可用于Windows 8.1和Windows Phone 8.1,并想知道如何在XAML代码中格式化字符串。
我尝试过的方法(XAML):
<TextBlock Text="{Binding TestItem.CurrentDate, StringFormat={}{0:MM/dd/yyyy}}" />
问题在于StringFormat
在Windows.UI.Xaml.Controls.TextBox
中不可用。
微软创建了一个示例项目,专门讲解日期格式化。但是,该示例中使用的方法基于(丑陋的)代码后台。
所以,这里是我的问题:
StringFormat
为什么在Windows存储通用应用程序中不可用?- 如何仅使用XAML代码格式化字符串?
编辑: 我决定使用转换器解决方案,对于那些感兴趣的人,这是代码:
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
if (!(value is DateTime))
return null;
var dateTime = (DateTime)value;
var dateTimeFormatter = new DateTimeFormatter(YearFormat.Full,
MonthFormat.Full,
DayFormat.Default,
DayOfWeekFormat.None,
HourFormat.None,
MinuteFormat.None,
SecondFormat.None,
new[] { "de-DE" },
"DE",
CalendarIdentifiers.Gregorian,
ClockIdentifiers.TwentyFourHour);
return dateTimeFormatter.Format(dateTime);
}
public object ConvertBack(object value, Type targetType, object parameter,
string language)
{
throw new NotImplementedException();
}
}
我很高兴能够得到任何改进上面代码的建议,欢迎留言。
感谢Mikael Dúi Bolinder和Martin Suchan提供的建议/答案。