WPF绑定语法的StringFormat [英] WPF binding StringFormat syntax

查看:445
本文介绍了WPF绑定语法的StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 值应四舍五入到整数(例:1,234)

  2. 当值是0.00,它应该显示为单个零。 (例如:0)

目前我用波纹管标记了格式化的十进制值,但它显示00时,该值为0.00。请帮助。

 <的TextBlock
  Grid.Column =6
  填充=2
  文本={绑定路径= TotalAwardsExpended,的StringFormat ='{} {0:0,0}'}/>


解决方案

额外的0来自0冒号后。

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

上的自定义数字字符串MSDN文档格式(强调):


  

0|零占位符|替换如果是present相应的数字零; ,否则,零出现在结果字符串


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}'}" />

解决方案

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.

这篇关于WPF绑定语法的StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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