我在纳米比亚工作.纳米比亚不是
Windows地区和语言设置的选项,但与南非共享大多数文化细节,因此我们选择英语南非,然后将货币符号自定义为“N $”(纳米比亚元)而不是“R”(南非兰特).
但是,我无法说服WPF使用自定义货币.在代码中使用string.format(“{0:c}”,foo)工作正常,但在XAML中使用{Binding Path = SomeCurrencyValue,StringFormat = c}`仍然使用“R”符号而不是自定义“N $”符号.
在App.xaml.cs中,我使用以下代码设置Application Culture:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.LCID,true); FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),new FrameworkPropertyMetadata( System.Windows.Markup.XmlLanguage.GetLanguage( System.Globalization.CultureInfo.CurrentUICulture.IetfLanguageTag)));
<StackPanel> <TextBlock> Formatted in XAML with: <LineBreak/> Text="{Binding Path=SomeCurrencyValue,StringFormat=c}" <LineBreak/> Result: </TextBlock> <TextBox Text="{Binding Path=SomeCurrencyValue,StringFormat=c,Mode=OneWay}" Margin="5"/> <TextBlock> Formatted in code with: <LineBreak/> return string.Format("{0:c}",SomeCurrencyValue); <LineBreak/> Result: </TextBlock> <TextBox Text="{Binding Path=SomeCurrencyString,Mode=OneWay}" Margin="5"/> </StackPanel>
上述View的DataContext包含以下内容:
public double SomeCurrencyValue { get { return 34.95; } } public string SomeCurrencyString { get { return string.Format("{0:c}",SomeCurrencyValue); } }
结果如下:
我知道有一个类似的问题here,但我希望通过一个更完整的问题得到更好的答案.我主要致力于为纳米比亚客户提供金融应用程序,所以对我来说这是一个非常严重的问题 – 如果用.NET 4.0无法做到这一点,我会考虑提交错误报告,但我只想先查看这里.
编辑:
对此的一个解决方案是创建这个纳米比亚CultureInfo,因此它被所有.NET层完全识别.这是一个代码:
public static void RegisterNamibianCulture() { // reference the sysglobl.dll assembly for this CultureAndRegionInfoBuilder namibianCulture = new CultureAndRegionInfoBuilder("en-NA",CultureAndRegionModifiers.None); // inherit from an existing culture namibianCulture.LoadDataFromCultureInfo(new CultureInfo("en-za")); namibianCulture.CultureEnglishName = "Namibia"; namibianCulture.RegionEnglishName = "Namibia"; namibianCulture.CultureNativeName = "Namibia"; // you may want to change this namibianCulture.RegionNativeName = "Namibia"; // you may want to change this // see http://en.wikipedia.org/wiki/ISO_3166-1,use user-defined codes namibianCulture.ThreeLetterISORegionName = "xna"; // I use x as 'extended' and 'na' as namibia namibianCulture.TwoLetterISORegionName = "xn"; namibianCulture.ThreeLetterWindowsRegionName = namibianCulture.ThreeLetterISORegionName; // see http://www.currency-iso.org/dl_iso_table_a1.xml namibianCulture.ISOCurrencySymbol = "nad"; namibianCulture.CurrencyEnglishName = "Namibia Dollar"; namibianCulture.CurrencyNativeName = "Namibia Dollar"; // you may want to change this // this is were you build something specific,like this symbol you need namibianCulture.NumberFormat.CurrencySymbol = "N$"; // you'll need admin rights for this namibianCulture.Register(); } public static void UnregisterNamibianCulture() { CultureAndRegionInfoBuilder.Unregister("en-NA"); }
一旦你在给定的机器上调用了Register函数一次(你需要在最终用户机器上安装这种文化),你现在可以使用你的初始WPF启动代码,只需改变它:
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-NA");
一切都应该按预期工作.您还可以使用标准语言标签和所有爵士乐,因为现在可以识别en-NA.