WPF 绑定 StringFormat 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3167833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:54:31  来源:igfitidea点击:

WPF binding StringFormat syntax

wpfbindingstring-formatting

提问by m00sila

How can I format a decimal value conditionally in a WPF window?

如何在 WPF 窗口中有条件地格式化十进制值?

  1. Value should be rounded to a whole number (Ex: 1,234)
  2. When the value is 0.00, it should display as a single zero. (Ex: 0)
  1. 值应四舍五入为整数(例如:1,234)
  2. 当值为 0.00 时,它应显示为单个零。(例如:0)

Currently I use bellow mark up to format the decimal value, but it displays 00 when the value is 0.00. Please help.

目前我使用波纹管标记来格式化十进制值,但当值为 0.00 时它显示 00。请帮忙。

<TextBlock
  Grid.Column="6"
  Padding="2"
  Text="{Binding Path=TotalAwardsExpended, StringFormat='{}{0:0,0}'}" />

回答by codekaizen

The extra 0 comes from the 0 after the colon.

额外的 0 来自冒号后的 0。

Instead, try {}{0:#,0}.

相反,尝试{}{0:#,0}

From the MSDN docs on Custom Numeric String formats(emphasis added):

来自关于自定义数字字符串格式MSDN 文档(强调添加):

"0" | Zero placeholder | Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

“0” | 零占位符 | 如果存在,则用相应的数字替换零;否则,结果字符串中将出现零