WPF: Textbox Binding with StringFormat={}{0:F2}. Don't show zero's

若如初见. 提交于 2019-12-10 12:39:20

问题


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:


Try the following:

StringFormat={}{0:#.##}

It will format to two decimal places and won't show zeroes.



来源:https://stackoverflow.com/questions/3725189/wpf-textbox-binding-with-stringformat-0f2-dont-show-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!