gpt4 book ai didi

c# - 我如何或可以为 TextBlock 上的 StringFormat 使用静态资源?

转载 作者:太空狗 更新时间:2023-10-29 18:17:46 25 4
gpt4 key购买 nike

我有一个显示日期/时间的文本 block 。时钟的外观在应用程序的某些控件上可能有所不同,就颜色和字体而言,但我希望日期和时间具有相同的格式。

我知道我可以像这样设置 StringFormat 属性:

<TextBlock Text="{Binding CurrentDateTime, StringFormat='{}{0:h\:mm tt}'}" Foreground="White" FontFamily="Proxima Nova Rg" FontSize="20" />

但是,我不知道如何将字符串格式提取到单独的字符串资源字典中。我尝试执行如下操作,但日期时间字符串根本没有出现。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="MyFormat">{}{0:h\:mm tt}</system:String>
</ResourceDictionary>

<!-- In another file -->
<TextBlock Text="{Binding CurrentDateTime, StringFormat={StaticResource MyFormat}}" Foreground="White" FontFamily="Proxima Nova Rg" FontSize="20" />

这完全可以做到吗?如果是,怎么办?

谢谢

最佳答案

看来您只需要删除 {}:

<system:String x:Key="MyFormat">{0:h\:mm tt}</system:String>

{} 是属性值所必需的,以防止它们被解释为标记扩展。但是,使用属性元素语法,您不再需要 {},因为大括号在该上下文中没有特殊含义。

关于c# - 我如何或可以为 TextBlock 上的 StringFormat 使用静态资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30743126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com