I am working with WPF and C# and I want to format a set of digits (not currency or number) with StringFormat in a TextBlock.
Example: 456321789 (string)
And I want to show it in the TextBlock like 456 321 789.
This examples doesn’t work:
<TextBlock Text="{Binding Number, StringFormat=000 000 000"/>
<TextBlock Text="{Binding Number, StringFormat=### ### ###"/>
<TextBlock Text="{Binding Number, StringFormat={}{0:000 000 000}"/>
<TextBlock Text="{Binding Number, StringFormat={}{0:### ### ###}"/>
Sometime ago I tried to format a set of 7 digits like 5432-123. And I wasn’t successful.
I tried many examples, and noting works with. What I am doing wrong?
Note: The only way I can achieve this is if I have the set of digits as an integer. But I’d like to understand why I can’t do it with a string.
Thanks in advance!
The string formats you are using are only applicable to integers, according to the documentation. So in order to get the desired formatting, you have to bind to a number. not a string.
If you think a little, there is a reason behind that: the formatting like digit grouping is only interesting for numbers and not to generic strings like “helloworld”. So noone cared about implementing such formats for generic string.
Sorry for that.
(A question aside: if your data is guaranteed to be a string representation of a number, why don’t you keep the value as an
intthen?)