8

Let's say I have a number 1234567.89. The number is displayed in a WPF TextBlock. I am trying to apply StringFormat attribute to the Text property so that the number would be displayed like:

1.234.567,89

As you can see, the thousand and decimal separators are inverted from the invariant culture specification.

I've tried setting numerous combinations for the StringFormat, but without success. This is the latest I came up with:

Text="{Binding SomeBinding, StringFormat={}{0:#'.'##0','00}}"

But the output isn't correct. Also, using N2 or changing culture isn't an option. I would like to avoid converters if possible.

So, is there a way to change the default separators through XAML?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Miloš Ranđelović
  • 444
  • 1
  • 5
  • 13

1 Answers1

13

You don't have to change the culture. Just use String.Format with specified culture (de-DE should be fine):

string output = String.Format(new CultureInfo("de-DE"), "{0:N}", yourDoubleValue);

Output: 9.164,32

If you want to do it in XAML, you could try:

Text="{Binding SomeBinding, StringFormat={}{0:N}, ConverterCulture=de-DE}"
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • I think he is trying to do all the stuff in XAML, and I don't see how this can be done without brining some C# in place, at least in the form of custom converter. – Woodman Feb 21 '13 at 11:01
  • The combination `StringFormat=N2, ConverterCulture=de` would create the desired output without any additional code. – Clemens Feb 21 '13 at 11:11
  • @Clemens thanks for comment. I cannot test the code, it was my suggestion – Andrey Gordeev Feb 21 '13 at 11:20
  • Working! Thanks. :) Just edit the post and put `StringFormat={}{0:N}` instead of `StringFormat={}{0:#'.'##0','00}`, not to confuse anybody. – Miloš Ranđelović Feb 21 '13 at 11:55
  • Note that there are issues with the Binding StringFormat not picking up the current culture automatically: http://stackoverflow.com/questions/991526/wpf-xaml-bindings-and-currentculture-display – Duncan Matheson Feb 22 '13 at 03:40