我玩弄新的窗口商店普遍应用程序模板,可用于Windows8.1和Windows电话8.1并想知道如何格式串XAML代码。

我尝试了(XAML):

 <TextBlock Text="{Binding TestItem.CurrentDate, StringFormat={}{0:MM/dd/yyyy}}" />

问题是, StringFormat 是不是在 Windows.UI.Xaml.Controls.TextBox.

微软已经创建了一个 样品项目 这是所有有关格式的日期。但是,使用的方法是基于(丑)代码后面。

所以,在这里是我的问题:

  • 为什么 StringFormat 不提供Windows商店普遍应用程序?
  • 如何格式串的反馈代码吗?


编辑: 我决定要走的转换器方案,为那些感兴趣的在这里是代码:

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();
    }
}

我很乐意为每一个建议如何改善上述代码,随意发表评论。

谢谢你,麦可Dúi莉亚和马丁Suchan对你的建议/答案。

有帮助吗?

解决方案

数据绑定在Windows运行的项目类型并不支持StringFormat财产的选项,必须是:

  • 已经使用的格式日期作为一种获得只 酒店在你的视图模型.
  • 使用 转换器 -你甚至可以创建StringFormatConverter在那里你可以通过DateTime的格式 ConverterParameter. 这里有一个解决方案 如何这样的StringFormatConverter可能的工作。
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top