wpf 使用 StringFormat 隐藏零值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19390176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide zero values with StringFormat
提问by Naty Bizz
I made a similar question months ago
几个月前我提出了类似的问题
In this case I have a textblock like this
在这种情况下,我有一个这样的文本块
<TextBlock FontSize="28" Text="{Binding DataPoint.Y, StringFormat=\{0:0\%\}}" Foreground="Black">
As you can see, my StringFormat puts a '%' sign after the number, if my data is 0.0 (I fill the component in code behind, my variable is a double) I get "0%"
正如你所看到的,我的 StringFormat 在数字后面放了一个 '%' 符号,如果我的数据是 0.0(我在后面的代码中填充了组件,我的变量是一个双精度)我得到“0%”
But now I want to get "" if my text is 0.0
但现在我想得到 "" 如果我的文字是 0.0
So far I have this:
到目前为止,我有这个:
Text="{Binding DataPoint.Y, StringFormat=\{0:#.#\%\}}"
But this retrieves "%", how can I get ""?
但这会检索“%”,我怎样才能得到“”?
采纳答案by Naty Bizz
This worked for me
这对我有用
{0:#\%;0:#;#}
回答by William
Use Custom Numeric Format Strings. Specifically, see the ";" Section Separator.
使用自定义数字格式字符串。具体见“;” 节分隔符。
Using {0:#.#\\%;-#.#\\%;}will give you the desired output, I believe.
{0:#.#\\%;-#.#\\%;}我相信,使用将为您提供所需的输出。
回答by Michael K
None of these worked for simple positive integers. I had a field which would always default to 0. To hide it, but not add any decimals places, I use:
这些都不适用于简单的正整数。我有一个总是默认为 0 的字段。要隐藏它,但不添加任何小数位,我使用:
Text="{Binding BuyerNumber, StringFormat=0;;#}"
Hope this helps someone out who is looking for integers only.
希望这可以帮助那些只寻找整数的人。

