Xaml 如何在Windows应用商店通用应用程序(W8.1+;WP8.1)中格式化日期?
我在玩新的Windows应用商店通用应用程序模板,它可以用于Windows 8.1和Windows Phone 8.1,我想知道如何在XAML代码中格式化字符串 我尝试的内容(XAML): 我很高兴收到关于如何改进上述代码的每一条建议,请随意评论Xaml 如何在Windows应用商店通用应用程序(W8.1+;WP8.1)中格式化日期?,xaml,windows-store-apps,windows-8.1,windows-phone-8.1,win-universal-app,Xaml,Windows Store Apps,Windows 8.1,Windows Phone 8.1,Win Universal App,我在玩新的Windows应用商店通用应用程序模板,它可以用于Windows 8.1和Windows Phone 8.1,我想知道如何在XAML代码中格式化字符串 我尝试的内容(XAML): 我很高兴收到关于如何改进上述代码的每一条建议,请随意评论 感谢Mikael Dúi Bolinder和Martin Suchan的建议/回答。Windows运行时项目类型中的数据绑定不支持StringFormat属性,您有以下选项: 在ViewModel中将已格式化的日期用作“仅获取”属性 使用转换器-您甚
感谢Mikael Dúi Bolinder和Martin Suchan的建议/回答。Windows运行时项目类型中的数据绑定不支持StringFormat属性,您有以下选项:
- 在ViewModel中将已格式化的日期用作“仅获取”属性
- 使用转换器-您甚至可以创建StringFormatConverter,在这里您可以将日期时间格式作为转换器参数传递。这种转换器是如何工作的
<TextBlock Text="{Binding TestItem.CurrentDate, StringFormat={}{0:MM/dd/yyyy}}" />
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();
}
}