WPF StringFormat={0:C} showing as dollars

こ雲淡風輕ζ 提交于 2019-11-26 05:28:58

问题


Why does this line of code

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

Output the result as $xx.xx when all my regional settings are set to UK. I expect it to output it as £xx.xx. Any ideas? I have tried different variations of the stringformat including StringFormat={}{0:C} but still get the same result.

Thanks for looking.


回答1:


I'm not sure if this has been fixed in .NET 4, but WPF has never picked up the current culture when rendering things like currency or dates. It's something I consider a massive oversight, but thankfully is easily corrected.

In your App class:

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

See this excellent post for more information.




回答2:


I do Language="en-GB" in the main window e.g.

<Window x:Class="AllocateWPF.Vouchers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="692" Width="1000" Language="en-GB">



回答3:


What works for me:
1) In app.xaml override OnStartup() and add - System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("et-EE");

2) Define in XAML @ Window level - xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"

3) In XAML - <TextBox Text="{Binding Path=Price, StringFormat='{}{0:C}', ConverterCulture={x:Static sysglb:CultureInfo.CurrentUICulture}}" />

This correctly picks up any custom regional settings. Although I'm using a manually created CultureInfo in the first step, I'm sure it's possible to pass in one of the static types - eg. System.Globalization.CultureInfo.CurrentCulture (I haven't tested it though...)



来源:https://stackoverflow.com/questions/2764615/wpf-stringformat-0c-showing-as-dollars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!