问题描述
限时送ChatGPT账号..有谁知道在 UWP Windows 10 应用中使用 x:Bind 时如何格式化日期?
Does anyone know how to format a date when using x:Bind in a UWP Windows 10 app?
我有一个 TextBlock,它绑定 (x:Bind) 到我的 ViewModel 上的 DateTime 属性,该属性是从 SQL 读取的.我想将输出格式化为dd/MM/yyy HH:mm (ddd)".有没有简单的方法可以做到这一点?
I have a TextBlock that is bound (x:Bind) to a DateTime property on my ViewModel which is read from SQL. I want to format the output to "dd/MM/yyy HH:mm (ddd)". Is there a simple way of doing this?
默认格式是dd/MM/yyy HH:mm:ss",我认为它来自默认格式.这个可以换吗?
The default format is "dd/MM/yyy HH:mm:ss" which I presume is coming from a default. Could this be replaced maybe?
谢谢.
推荐答案
Use a StringFormatConverter
(检查您是否可能使用一些已经包含它的库,例如 UWP 工具包(感谢@maxp)或更旧的Cimbalino Toolkit):
Use a StringFormatConverter
(check if you maybe use some library, which already includes it, e.g. the UWP Toolkit (thanks, @maxp) or the older Cimbalino Toolkit):
public class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
if (parameter == null)
return value;
return string.Format((string)parameter, value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
将其添加到您的页面资源
add it to your page resource
<Page.Resources>
<converters:StringFormatConverter x:Key="StringFormatConverter" />
</Page.Resources>
并像这样使用它
<TextBlock Text="{x:Bind Text, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd/MM/yyy HH\\\\:mm (ddd)}'}" />
这篇关于使用 UWP 和 x:Bind 的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论