WPF:使用 StringFormat={}{0:F2} 的文本框绑定。不要显示零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3725189/
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
WPF: Textbox Binding with StringFormat={}{0:F2}. Don't show zero's
提问by André Hauptfleisch
I am binding an object to a TextBox with the following XAML:
我正在使用以下 XAML 将对象绑定到 TextBox:
<TextBox Name="MyTextBox" Text="{Binding Path=MyValue, Mode=TwoWay, StringFormat={}{0:F2}}" />
<TextBox Name="MyTextBox" Text="{Binding Path=MyValue, Mode=TwoWay, StringFormat={}{0:F2}}" />
Naturally when I bind a new object (which values are all still zero) the Text property is set to 0.00
. I have several of these TextBoxes, which makes it tedious to delete every value before entering a new one.
自然地,当我绑定一个新对象(其值仍然为零)时,Text 属性设置为0.00
. 我有几个这样的文本框,这使得在输入新值之前删除每个值变得乏味。
At the moment I'm clearing these boxes in the Window_Loaded
method using the FindVisualChildrenmethod.
目前我正在Window_Loaded
使用FindVisualChildren方法清除方法中的这些框。
It just feels clunky though. Is there a neat way of doing this?
只是感觉很笨重。有没有一种巧妙的方法来做到这一点?
回答by kiwipom
Try the following:
请尝试以下操作:
StringFormat={}{0:#.##}
StringFormat={}{0:#.##}
It will format to two decimal places and won't show zeroes.
它将格式化为两位小数并且不会显示零。