質問

私は新しいWindows Store Universal Appテンプレートで遊んでWindows 8.1とWindows Phone 8.1に使用できるため、XAMLコードで文字列のフォーマット方法を疑問に思いました。

私が試したもの(xaml):

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

問題は、StringFormatWindows.UI.Xaml.Controls.TextBoxで利用できないことです。

マイクロソフトはサンプルプロジェクトはすべてフォーマット日付に関するすべてです。しかし、そこで使用されたアプローチは、後ろの(醜い)コードに基づいています。

だから、ここに私の質問です:

  • Windows Store Universal Appsでは、StringFormatが利用できないのはなぜですか?
  • 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();
    }
}
.

私は上記のコードを改善する方法のすべてのアドバイスに満足しています。

あなたの提案/回答のためにMikaelDảIBolinderとMartin Suhtan。

役に立ちましたか?

解決

WindowsランタイムのプロジェクトタイプでのデータバインディングはStringFormatプロパティをサポートしていません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top