用+和格式号 - 号 [英] Format number with + and - sign

查看:175
本文介绍了用+和格式号 - 号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StackPanel中一些WPF的TextBlocks,我想数据绑定和格式。

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.

问候,

米歇尔

推荐答案

试试这个:

Text="{Binding TestInt1, StringFormat={}{0:#;-#}}".

这篇文章有INT格式化好的样品 - <一个href=\"http://www.csharp-examples.net/string-format-int/\">http://www.csharp-examples.net/string-format-int/

这篇关于用+和格式号 - 号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆