StringFormat 值为零

Nat*_*izz 1 wpf xaml textblock string-formatting componentart

我正在使用componentart开发一个 wpf 应用程序我有一个这样的文本块

<TextBlock FontSize="28" Text="{Binding DataPoint.Y, StringFormat=\{0:#\\%\}}" Foreground="Black">
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我的 StringFormat 在数字后面放了一个 '%' 符号,但是如果我的数据是 0.0(我在后面的代码中填充了组件,我的变量是一个双精度)我得到“%”,但我想得到“ 0%”,我该怎么做?

dko*_*ozl 6

你可以使用不同的StringFormat. 而不是0:#put 0:0

<TextBlock FontSize="28" Text="{Binding Path=MyDouble, StringFormat=\{0:0\\%\}}" Foreground="Black"/>
Run Code Online (Sandbox Code Playgroud)

引用MSDN

#:如果存在“#”符号,则将其替换为相应的数字;否则,结果字符串中不会出现数字。

0:如果存在 1,则用相应的数字替换 0;否则,结果字符串中将出现零。