使用 UWP 和 x:Bind 的字符串格式

编程入门 行业动态 更新时间:2024-10-27 18:24:03
本文介绍了使用 UWP 和 x:Bind 的字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-28 10:10:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   格式   UWP   Bind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!