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.

Coesy
  • 936
  • 1
  • 10
  • 30

3 Answers3

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.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • 1
    Actually, according to [this bug report at MS Connect](https://connect.microsoft.com/VisualStudio/feedback/details/442569/wpf-binding-uses-the-wrong-currentculture-by-default), 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. – gehho May 04 '10 at 13:21
  • 8
    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? – Joey Dec 10 '10 at 00:48
  • In my case I didn't use it inside "OnStartUp" event. Just putting it before my "trouble window" loaded, was just enough. Thank you. – itsho May 27 '13 at 21:50
  • 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? – Rob Perkins Mar 01 '20 at 01:10
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">
Michael Harper
  • 1,531
  • 2
  • 25
  • 42
  • 1
    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" – Andrea Antonangeli Jan 29 '16 at 17:45
  • 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. – Andrea Antonangeli Nov 03 '18 at 15:49
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...)

Marko
  • 2,266
  • 4
  • 27
  • 48
  • 2
    This did solve the custom settings problem. For step 1 I used "= new CultureInfo(CultureInfo.CurrentCulture.IetfLanguageTag)" instead of hard-coding it. – avenmore Oct 01 '14 at 11:31