简体   繁体   中英

Change Currency symbol/Datetime format in WPF based on Culture

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>

You may get all the related CultureInfo using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM