Windows Store Universal Appで日付をフォーマットする方法(W8.1 + WP8.1)
-
21-12-2019 - |
質問
私は新しいWindows Store Universal Appテンプレートで遊んで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
で利用できないことです。
マイクロソフトはサンプルプロジェクトはすべてフォーマット日付に関するすべてです。しかし、そこで使用されたアプローチは、後ろの(醜い)コードに基づいています。
だから、ここに私の質問です:
- 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プロパティをサポートしていません。
- 既にフォーマットされている日付をget-only プロパティとして使用してください。
- converter を使用してください。 converterParameter として日時形式を渡すことができるStringFormatConverterを作成できます。これは、そのようなStringFormatConverterがどのように機能する可能性があるソリューションです。
所属していません StackOverflow