如何将 WPF 字符串格式设置为不乘以 100 的百分比?

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

How to set WPF string format as percent wihout multiplying by 100?

wpfdata-binding

提问by Emad Gabriel

I have a textbox bound to a property in an object. I have setup the string format to be p0.

我有一个绑定到对象中的属性的文本框。我已将字符串格式设置为 p0。

However, when I enter 12 for example it is formatted as 1200% (multiplies by 100 and add % sign)

但是,例如,当我输入 12 时,它的格式为 1200%(乘以 100 并添加 % 符号)

How can i set the stringformat so that for exampe 20 is formatted as 20% ?

如何设置 stringformat 以便例如将 20 格式化为 20% ?

My current control is :

我目前的控制是:

<TextBox Text="{Binding Path=MyCase, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=p0}"/>

how t change the string format so that the format for 7 is 7% not 700% ?

如何更改字符串格式,使 7 的格式为 7% 而不是 700% ?

回答by Thomas Levesque

"{Binding Path=Percentage, StringFormat={}{0}%}"

回答by Contango

Another solution is to wrap the %in single quotes, and put it inside the curley brackets:

另一种解决方案是将%用单引号括起来,并将其放在大括号内:

<TextBlock Text="{Binding Percentage, StringFormat={}{0:#0.00'%'}}"/>