wpf wpf中的字符串格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16439402/
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
String format in wpf
提问by Hodaya Shalom
I have a TextBoxon WPFthat is related to the float variable in my model
我有一个TextBox上WPF了在我的模型相关的浮点型变量
Following the TextxBox:
在 TextxBox 之后:
<TextBox Text="{Binding Position, StringFormat=f4}"/>
I want that TextBox will display a maximum 4 numbers after the point.
我希望 TextBox 在点后最多显示 4 个数字。
So I put StringFormat=f4.
所以我把StringFormat=f4.
But now, even when I have less than 4 numbers after the point and when I have a whole number it displays with 4 digits after the point.
但是现在,即使我在点后的数字少于 4 个并且当我有一个整数时,它也会在点后显示 4 位数字。
For example, the number 0 is shows that: 0.0000
例如,数字 0 表示: 0.0000
I want as long as it did not pass the four numbers, display it in a normal way, how can I do this?
我要只要不通过这四个数字,就正常显示,我该怎么做?
回答by Klaus78
you could try with StringFormat="{}{0:0.####}"
你可以试试 StringFormat="{}{0:0.####}"
This syntax with {}is due to the fact that we set a WPF property equal to a string that contains curly bracket symbols. Curly bracket symbols are interpreted by WPF in a particular way and would not be interpreted as part of the string. Without the {}the code would not compile. {}allows you to set a WPF to a string value that contains curly bracket symbols.
这种语法{}是因为我们将 WPF 属性设置为等于包含大括号符号的字符串。大括号符号由 WPF 以特定方式解释,不会被解释为字符串的一部分。没有{}代码将无法编译。{}允许您将 WPF 设置为包含大括号符号的字符串值。
You can have for example a look at the link String formatting in WPF and Silverlight
例如,您可以查看WPF 和 Silverlight 中的链接字符串格式
回答by greg
Take a look at this link about Custom Numeric Format Strings. I think this is what you might be looking for.
看看这个关于自定义数字格式字符串的链接。我想这就是你可能正在寻找的。
Or alternatively, try this;
或者,试试这个;
<TextBox Text="{Binding Position, StringFormat='{}{0:#,0000}'}"/>
Hope this helps! :)
希望这可以帮助!:)
EDIT:
编辑:
This previous question might help also;
上一个问题也可能有所帮助;

