2013-04-11 47 views
0

在App.xaml.cs我:WPF,改變當前文化的NumberFormatInfo不起作用

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; 

現在在不同的窗口,這將在主窗口中打開,我寫了下面:

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

但結果是一樣的東西$ 1,200.00,不是我預計:1,200美元。這裏有什麼問題?

回答

0

可能有更聰明的方法,但這應該工作始終...

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

而且在應用程序啓動或構造函數移動這個...

然後做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

+0

您的代碼可以工作,但我想要實現動態是打開一個特定的文化數據庫時更改的NumberFormat。您的代碼需要重新啓動應用程序。 – synergetic 2013-04-12 00:33:34

0

NumberFormatting是一個使用CurrentCulture的操作(默認情況下,除非您明確地向該方法提供Culture或NumberFormatter)而不是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; 
+1

我查過了; nfi.IsReadOnly等於false。代碼有效; CurrencyDecimalDigits從2變爲0.我也複製/粘貼您的代碼,但沒有任何更改。 – synergetic 2013-04-12 00:27:27