Reputation: 2879
I have some WPF textblocks in a stackpanel that I want to databind and format.
E.g. the following formats a date 24h style without the seconds part:
<TextBlock Text="{Binding MyCustomObject, StringFormat={}{0:HH:mm}}" />
Now, I would like to bind an integer and also display the + and - sign (i.e. +6 or -4).
<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#}}" />
This however, does not work. Is this possible or do I have to write a complete converter just for this?
EDIT
Nikolays post led me to the answer:
<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" />
In essence you provide a format for positive numbers, negative numbers and an optional part what to do with zero. In this case I stated that a zero should be displayed as an empty string.
Regards,
Michel
Upvotes: 8
Views: 7376
Reputation: 3828
Try this:
<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" />
This article has nice samples of int formatting - http://www.csharp-examples.net/string-format-int/
Upvotes: 17