1

All the articles on the internet suggest that to make the XAML binding detect the selected culture in the operating system, this line should be used.

 FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata((XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))));

The problem is that the regional settings in windows allow the user to customize the formatting of dates and numbers in any way the user wants. For example, lets say that the user wants to 3 decimal digits instead of 2. he goes to the regional settings and makes this update. The problem is that this change is not picked up in Xaml binding

<TextBlock Text="{Binding Payments, StringFormat=C}" /> <!--display 2 digits-->

Oddly enough, in the code behind, if i use ToString("C") i will successfully get whatever customization i did on the server side.

decimal sampleNumber = 1.123;
string s = sampleNumber.ToString("C"); //Puts 3 digits in the string variable

My workaround is to use a custom converter where i do the ToString and return the string to the binding. But it is weird that i haven't seen this problem (which is most probably a bug in WPF) posted anywhere on the net. Am i missing something?

Fadi
  • 55
  • 1
  • 10
  • Here is a similar [post][1] and the solution in code seems to work. [1]: http://stackoverflow.com/questions/2764615/wpf-stringformat-0c-showing-as-dollars – anivas Jul 28 '11 at 09:56
  • I'm not complaining about not being able to detect the selected culture. This works OK. I'm complaining about not being able to detect any customizations that you make to your selected culture. – Fadi Jul 28 '11 at 10:08

1 Answers1

3

I'm not sure if it works for you, but I remember once I solved something like this but regarding date formats. I used ConverterCulture attribute for bindings:

<TextBox Text="{Binding Path=Date, ConverterCulture={x:Static Globalization:CultureInfo.CurrentCulture}}"/>
Kreol
  • 365
  • 5
  • 13
  • Wow! It worked. Do you know what could be the reasoning behind it? – Fadi Jul 28 '11 at 11:26
  • Do you think that it would be possible to find a way to apply this on application level? – Fadi Jul 28 '11 at 11:33
  • The reason as far as I know is just setting of CurrentCulture hasn't bee done by default for ConverterCulture. There are lot of complaints about it. I don't know if there is some serious reason behind it.. but I'd say it's just bug/oversight. – Kreol Jul 28 '11 at 11:37
  • To apply it on application level... well.. one could write their own xaml markup extension probably :) But seriously hard to say.. don't know at the moment – Kreol Jul 28 '11 at 11:40
  • Maybe some utility method could be implemented that in somewhere in code behind goes through all bindings and sets theirs ConverterCulture – Kreol Jul 28 '11 at 11:46
  • Thanks, Kreol. I hope they'll fix it in some new .net version – Fadi Jul 28 '11 at 11:59
  • Thanks a lot. I said Text="{Binding Gain, Mode=OneWay, ConverterCulture='en-US', StringFormat={}{0:0.000}}" in non en-US culture and displayed value in en-US format. It worked fine! – Demir Jan 03 '14 at 12:16