WPF binding StringFormat syntax

此生再无相见时 提交于 2019-11-27 03:11:37

问题


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

  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)

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

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

回答1:


The extra 0 comes from the 0 after the colon.

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

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

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



来源:https://stackoverflow.com/questions/3167833/wpf-binding-stringformat-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!