Setting Culture (en-IN) globally in WPF application

Varun Jain picture Varun Jain · Sep 17, 2011 · Viewed 49.4k times · Source

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

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

The above code is called before the Window.InitializeComponent() method is called.

Still this is showing $ as CurrencySymbol in all TextBoxes.

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

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

Answer

eandersson picture eandersson · Sep 17, 2011

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)));
}