2

I have a requirement which need the formatting of DateTime based on the system's language setting and the Currency symbol based on the user setting. The user setting is from a combo box where a user chooses the country. So, if the user chooses UK then the currency should be displayed in pounds and the date time should be displayed in US format.

For Example: If the System setting is US and the User setting is UK then the DateTime format needs to be in US and the Currency symbol to be £.

I have used this code to change the Culture info based on the system locale settings:

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

How do I format the currency symbol based on the user setting. I have used the culture formatting StringFormat=C2 through out the application and it displays the currency symbol based on the system's locale setting.

This application is WPF.

Some of the countries included in the combobox are: USA, Germany, UK, Canada

Edit:

I could use something like this. But is there a way to bind these culture info from my view model?

   <StackPanel Margin="10">
        <TextBlock Text="{Binding TotalPrice, StringFormat=C2, ConverterCulture='en-UK'/>
        <TextBlock Text="{Binding TotalPrice, StringFormat=C2, ConverterCulture='en-US'/>
        <TextBlock Text="{Binding TotalPrice, StringFormat=C2, ConverterCulture='de-DE'/>
    </StackPanel>
Vinshi
  • 313
  • 2
  • 7
  • 27
  • How do you define a "user setting"? How is this different from the "system setting"? – mm8 Mar 14 '19 at 12:54
  • @mm8 The User setting is defined by changing the country in a combobox. So, if the user selects UK then all the currency should be displayed in pound. The application's Date format should still show US formatting. – Vinshi Mar 14 '19 at 14:54
  • There is no ComboBox mentioned in your questions, is it? You may want to edit it to clarify your requirements. Where are the currency symbols that you want to change dynamically for example? – mm8 Mar 14 '19 at 15:08
  • @mm8 Updated the question – Vinshi Mar 14 '19 at 15:25

1 Answers1

0

You may get all the related CultureInfousing CultureInfo.CurrentCulture.

From the documentation:

Windows allows users to override the standard property values of the CultureInfo object and its associated objects by using Regional and Language Options in Control Panel. The CultureInfo object returned by the CurrentCulture property reflects these user overrides in the following cases: [...]

After obtaining the CultureInfo object, you can get the currency symbol with its NumberFormat like this:

Console.WriteLine(currentCulture.NumberFormat.CurrencySymbol)
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • I do not want to override the culture info. I need that to change the DateTime. But is there another way to change the Currency based on user info. – Vinshi Mar 13 '19 at 19:54
  • Unfortunately no. WPF uses the `System.Windows.Application.Current.Dispatcher.Thread.CurrentUICulture` that just sets the locale of the thread the dispatcher is running. You COULD create your own Culture that sets another currency symbol and defines a DateTime format – Venson Mar 13 '19 at 22:46
  • @Vinshi It is not to override/change it. And you can still create a CultureInfo object based on what you have anyway, without changing anything of the system. – Luke Vo Mar 13 '19 at 23:31
  • @JPVenson Can you please show with code how to set for another culture – Vinshi Mar 14 '19 at 14:06
  • @Vinshi i have to kindly Direct you to another StackOverflow question related to this topic. https://stackoverflow.com/questions/12916940/creating-custom-cultureinfo-for-country-language-combination – Venson Mar 14 '19 at 23:02