我使用以下 XAML 将对象绑定到 TextBox:
当然,当我绑定一个新对象(其值仍然为零)时,Text 属性设置为 0.00
。我有几个这样的文本框,这使得在输入新值之前删除每个值变得很乏味。
目前,我正在使用 FindVisualChildren 方法。
但它只是感觉很笨重。有没有一种巧妙的方法来做到这一点?
I am binding an object to a TextBox with the following XAML:
<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.
At the moment I'm clearing these boxes in the Window_Loaded
method using the FindVisualChildren method.
It just feels clunky though. Is there a neat way of doing this?
发布评论
评论(1)
请尝试以下操作:
StringFormat={}{0:#.##}
它将格式化为两位小数,并且不会显示零。
Try the following:
StringFormat={}{0:#.##}
It will format to two decimal places and won't show zeroes.