WPF StringFormat={0:C} showing as dollars
Asked Answered
A

3

48

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.

Africa answered 4/5, 2010 at 10:52 Comment(0)
K
75

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.

Keyek answered 4/5, 2010 at 11:3 Comment(4)
Actually, according to this bug report at MS Connect, it is not a bug, but a feature as MS states... Pretty weird feature, I would say, but it might be good to know that this is unlikely to be "fixed" in a future version.Monodic
This won't pick up custom changes to the regional settings though (i.e. I'm using German, but with a sane date format [ISO 8601]). Is there a workaround for that too?Ciri
In my case I didn't use it inside "OnStartUp" event. Just putting it before my "trouble window" loaded, was just enough. Thank you.Median
The bug report link to MS Connect is gone, because MS Connect is gone. Is there a valid link to the MS Connect discussion about invariant culture as a feature in WPF?Predominant
C
36

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">
Chert answered 23/9, 2013 at 16:29 Comment(2)
Life saver, I love you. To show Euro sign and dots intead of commas (Italian culture): Language="it-IT" in XAML, then format the string with "€ #,##0.00"Infinitive
In case you need to format a DataGridTextColumn, use this: Binding="{Binding Path=PrezzoListino, ConverterCulture='it-IT', StringFormat='\{0:€ #,##0.00\}'}" in the DataGridTextColumn. Example is for Italian culture, Euro currency and "PrezzoListino" field to bind to.Infinitive
D
20

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...)

Diaspore answered 24/8, 2012 at 19:21 Comment(1)
This did solve the custom settings problem. For step 1 I used "= new CultureInfo(CultureInfo.CurrentCulture.IetfLanguageTag)" instead of hard-coding it.Quinque

© 2022 - 2024 — McMap. All rights reserved.