c# - WPF 本地化 : DynamicResource with StringFormat?

标签 c# .net wpf localization

我正在使用 ResourceDictionary 在 .NET 4 中进行本地化。有没有人有使用字符串格式的值的解决方案?

例如,假设我有一个键为“SomeKey”的值:

<ResourceDictionary ...>
    <s:String x:Key="SomeKey">You ran {0} miles</s:String>
</ResourceDictionary>

在 TextBlock 中使用它:

<TextBlock Text="{DynamicResource SomeKey}" />

例如,我如何将整数与 SomeKey 的值组合为格式字符串?

最佳答案

您需要以某种方式绑定(bind)到 ViewModel.Value,然后使用(嵌套)绑定(bind)到格式字符串。

当你只有一个值时:

<TextBlock 
  Text="{Binding Path=DemoValue, StringFormat={StaticResource SomeKey}}" />        

当您还有 {1} 等时,您就需要 MultiBinding。

编辑:

当您真的想在实时窗体中更改语言时,明智的方法可能是在 ViewModel 中进行所有格式化。无论如何,我很少在 MVVM 中使用 StringFormat 或 MultiBinding。

关于c# - WPF 本地化 : DynamicResource with StringFormat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925145/

相关文章:

c# - 在 WPF 媒体元素中连续重播视频

wpf - XAML 中绑定(bind)路径属性的串联

c# - 在 C# 中创建图像

c# - 读取大型 XLSX 文件

c# - 名称 `Array'在当前上下文中不存在

.net - 如何使用 MySQL 设置 Identity Server 4

c# - Long DateTime 的 Linq to Sql 问题

c# - 更改默认悬停文本颜色 wpf

c# - Dictionary<myField, myObject> 其中 myField 是 myObject 的成员

c# - Xamarin UI 测试 "1 is not a supported code page."