用 + 和 - 符号格式化数字 [英] Format number with + and - sign
问题描述
我想对堆栈面板中的一些 WPF 文本块进行数据绑定和格式化.
I have some WPF textblocks in a stackpanel that I want to databind and format.
例如以下格式不带秒部分的日期 24 小时样式:
E.g. the following formats a date 24h style without the seconds part:
<TextBlock Text="{Binding MyCustomObject, StringFormat={}{0:HH:mm}}" />
现在,我想绑定一个整数并显示 + 和 - 符号(即 +6 或 -4).
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?
编辑
Nikolays 的帖子让我找到了答案:
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.
问候,
米歇尔
推荐答案
试试这个:
<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" />
这篇文章有很好的 int 格式示例 - http://www.csharp-examples.net/string-format-int/
This article has nice samples of int formatting - http://www.csharp-examples.net/string-format-int/
这篇关于用 + 和 - 符号格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!