WPF,更改当前文化的NumberFormatInfo不起作用

时间:2013-04-11 11:06:00

标签: wpf globalization

在App.xaml.cs中我有:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), 
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(
        CultureInfo.CurrentCulture.IetfLanguageTag)));

在我的MainWindow.xaml.cs中,我有:

NumberFormatInfo nfi = System.Threading.Thread.CurrentThread
                       .CurrentCulture.NumberFormat;
nfi.CurrencySymbol = "USD";
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyDecimalDigits = 0;

NumberFormatInfo nfi = System.Threading.Thread.CurrentThread
                       .CurrentUICulture.NumberFormat;
nfi.CurrencySymbol = "USD";
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyDecimalDigits = 0;

现在在另一个将从MainWindow打开的窗口中,我写了以下内容:

<TextBox Text="{Binding Total, StringFormat=c}"

但结果是1,200.00美元,不是我的预期:USD1,200。这有什么不对?

2 个答案:

答案 0 :(得分:0)

可能有更聪明的方法,但这应该是一致的......

// xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"
<TextBox Text="{Binding Total, StringFormat='{}{0:C}', ConverterCulture={x:Static sysglb:CultureInfo.CurrentUICulture}}" />  

然后在app启动或ctor移动...

然后执行OverrideMetadatabase.OnStartup之前 - 例如......

protected override void OnStartup(StartupEventArgs e)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

    NumberFormatInfo nfi = System.Threading.Thread.CurrentThread
                            .CurrentCulture.NumberFormat;
    nfi.CurrencySymbol = "USD";
    nfi.CurrencyDecimalSeparator = ".";
    nfi.CurrencyDecimalDigits = 0;

    nfi = System.Threading.Thread.CurrentThread
                            .CurrentUICulture.NumberFormat;
    nfi.CurrencySymbol = "USD";
    nfi.CurrencyDecimalSeparator = ".";
    nfi.CurrencyDecimalDigits = 0;

    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
        new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(
            CultureInfo.CurrentCulture.IetfLanguageTag)));

    base.OnStartup(e);

}

另见:

WPF StringFormat={0:C} showing as dollars

答案 1 :(得分:0)

NumberFormatting是一个使用CurrentCulture的操作(默认情况下,除非您向方法明确提供Culture或NumberFormatter)NOT CurrentUICulture。 CurrentUICulture用于资源(本地化)查找。

从CurrentCulture获取的NumberFormatInfo应该是只读的(see Remarks of NumberFormatInfo documentation),并且当您尝试设置任何属性(see Remarks of NumberFormatinfo.IsReadOnly)时应该抛出InvalidOperationException。 (您是否吞下了这个例外?)如果您创建了一个可写的CultureInfo并更改了它的属性,那么您可以将其设置为当前的文化。

您需要执行以下操作:

CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
NumberFormatInfo nfi= ci.NumberFormat;
nfi.CurrencySymbol = "USD";
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyDecimalDigits = 0;
// set the default culture for all threads that haven't been explicitly set (.Net 4.5)
CultureInfo.DefaultThreadCurrentCulture = ci;
// or the following if you aren't using .Net 4.5
// Thread.CurrentThread.CurrentCulture = ci;