none
Using Custom Regional Settings in WPF

    Question

  • I am creating a localized WPF application and i want WPF to use whatever settings i have in my regional settings.

    All over the internet they recommend the following code, which i did use

    protected override void OnStartup(StartupEventArgs e)
    {
      FrameworkElement.LanguageProperty.OverrideMetadata(
        typeof(FrameworkElement),
        new FrameworkPropertyMetadata(
          XmlLanguage.GetLanguage(
          CultureInfo.CurrentCulture.IetfLanguageTag)));
      base.OnStartup(e);
     }
    

    Now, let's suppose that in the regional settings i'm still using en-US but i have decided to show the currency as 1 digit after the decimal point instead of 2

    If i use this code, it works perfectly

    decimal d = 2.5M;
    MessageBox.Show(d.ToString("C"));
    

    but when i use binding, i still get both digits after the decimal point

    <TextBlock Text="{Binding Net, StringFormat=c}"/>
    

    Now if i change the locale to fr-FR, the currency symbol changes in both ToString and WPF binding, which proves that the code i added on the StartUp is working. Without this code the binding will not take the selected locale in the regional settings into consideration.

    It looks like the code that we are using in the StartUp event loads the default settings of selected language in the regional settings, so any modifications that i make after selecting the language will not be taken into consideration in WPF binding.

    Am i missing something, or this is a bug in WPF localization?

     

    Thank you for any Feedback

    Fadi

    Friday, October 8, 2010 10:47 PM

All replies

  • Hi Fadi,

    I have discussed this issue here:
    http://shujaatsiddiqi.blogspot.com/2010/08/wpf-localization-stringformat-for.html

    If you introduce ConverterCulture in your binding, it should fix this.

    This issue is also reported here:

    https://connect.microsoft.com/VisualStudio/feedback/details/442569/wpf-binding-uses-the-wrong-currentculture-by-default?wa=wsignin1.0

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com

    Saturday, October 9, 2010 1:48 AM
  • Dear Muhammad,

     

    Thanks for your reply.

    It looks like that you have misunderstood my point. I can get the currency in my binding to be to be localized, but i don't want the default localization.

    For example in the regional settings if you change to fr-FR, you will get the Euro symbol and 2 digits after the decimal point. In my case i just want 1 digit after the decimal point. so i go to the regional settings and change that. The ToString("C") gets affected, but the binding doesn't.

    The bug that you have mentioned is just about adding the code on StartUp, and the ConverterCulture is not practical anyway because i can't put a text block for all cultures.

     

    Thanks,

    Fadi

    Saturday, October 9, 2010 9:35 AM