Setting Culture (en-IN) Globally in WPF App

2019-01-07 16:30发布

I have an App, which is based for India, and I'm setting Culture as:

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

The above code is being set before the Window - InitializeComponent method is called.

Still this is showing $ as CurrencySymbol in all the TextBox.

If I bind the TextBox as following, it shows 'Rs.' as CurrencySymbol:

Text="{Binding Salary,Mode=TwoWay,StringFormat=C,ConvertCulture=en-IN}".

4条回答
走好不送
2楼-- · 2019-01-07 16:48

It is working for me, add this to your app.xaml.cs file startupmethod

Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.GetCultureInfo("en-IN");
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), 
            new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
查看更多
Deceive 欺骗
3楼-- · 2019-01-07 16:52

For me just works, if i put this code to the OnStartup overrided method:

public partial class App : Application
{
      public App()
      {                
      }

      protected override void OnStartup(StartupEventArgs e)
      {
          var vCulture = new CultureInfo("de-DE");

          Thread.CurrentThread.CurrentCulture = vCulture;
          Thread.CurrentThread.CurrentUICulture = vCulture;
          CultureInfo.DefaultThreadCurrentCulture = vCulture;
          CultureInfo.DefaultThreadCurrentUICulture = vCulture;

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

          base.OnStartup(e);
      }
}
查看更多
爷的心禁止访问
4楼-- · 2019-01-07 17:05
Thread.CurrentThread.CurrentCulture = 
    System.Globalization.CultureInfo.GetCultureInfo("en-IN");

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

This will switch the default language for the entire application. You’ll want to use this only in startup code as this setting can be applied only once per application. You can still override individual forms when necessary as below

this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

All WPF elements include a Language property that can be assigned and determines the Culture that is used for formatting.

Reference

查看更多
迷人小祖宗
5楼-- · 2019-01-07 17:08

I think you will need to add the following.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-IN");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
            XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

Read more here:

http://www.west-wind.com/weblog/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting

Just to give you an example, this is how I initialize the Culture in my program, based on the user setting, but you can simply replace UserSettings.DefaultCulture and UserSettings.Default.UICultrue with your wanted Culture.

private static void InitializeCultures()
{
    if (!String.IsNullOrEmpty(UserSettings.Default.Culture))
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(UserSettings.Default.Culture);
    }
    if (!String.IsNullOrEmpty(UserSettings.Default.UICulture))
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserSettings.Default.UICulture);
    }

    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
查看更多
登录 后发表回答