Format number with the + and - symbol

I have some WPF text blocks on the stack that I want to bind and format.

eg. the following formats: 24h date format without a second part:

<TextBlock Text="{Binding MyCustomObject, StringFormat={}{0:HH:mm}}" /> 

Now I would like to bind an integer and also display the + and - sign (e.g. +6 or -4).

 <TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#}}" /> 

This, however, does not work. Is this possible or do I need to write a complete converter just for this?

EDIT

Nicholas's message led me to the answer:

 <TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" /> 

Basically you provide a format for positive numbers, negative numbers and an optional part of what to do with zero. In this case, I indicated that zero should be displayed as an empty string.

Hi,

Michelle

+4
source share
1 answer

Try the following:

 <TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" /> 

There are good int formatting examples in this article - http://www.csharp-examples.net/string-format-int/

+9
source

Source: https://habr.com/ru/post/1402366/


All Articles