简体   繁体   English

根据文化在WPF中更改货币符号/日期时间格式

[英]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. 我有一个需要基于系统语言设置的DateTime格式和基于用户设置的Currency符号的格式的要求。 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 £. 例如:如果系统设置为US,用户设置为UK,则DateTime格式必须为美国,货币符号为£。

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. 我在整个应用程序中都使用了文化格式化StringFormat=C2 ,它根据系统的语言环境设置显示货币符号。

This application is WPF. 该应用程序是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 . 你可能会得到所有相关CultureInfo使用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. Windows允许用户使用“控制面板”中的“区域和语言选项”来覆盖CultureInfo对象及其关联对象的标准属性值。 The CultureInfo object returned by the CurrentCulture property reflects these user overrides in the following cases: [...] 在以下情况下,CurrentCulture属性返回的CultureInfo对象反映了这些用户替代:[...]

After obtaining the CultureInfo object, you can get the currency symbol with its NumberFormat like this: 获取CultureInfo对象后,您可以使用其NumberFormat获得货币符号,如下所示:

Console.WriteLine(currentCulture.NumberFormat.CurrencySymbol)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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