将输入文本框设为货币格式 (WPF MVVM)

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

Make input Textbox as currency format (WPF MVVM)

wpfmvvm

提问by user2473454

i am new in WPF, just start learning today. can anyone help me how to format the TextBox as currency format? which in my Textbox, can only be inputted by the numbers with 2 decimal point? Thanks.

我是 WPF 新手,今天才开始学习。谁能帮助我如何将文本框格式化为货币格式?在我的Textbox中,只能输入2个小数点的数字?谢谢。

回答by rhe1980

Are you looking for something like this?:

你在寻找这样的东西吗?:

<TextBox Text="{Binding Path=Txt, StringFormat=C}"/>

回答by eMko

You can use something like this

你可以使用这样的东西

 <TextBox TextAlignment="Right"
      Text="{Binding Price,
           UpdateSourceTrigger=PropertyChanged,
           StringFormat='#.00',
           ConverterCulture={x:Static sysglb:CultureInfo.CurrentCulture}}"/>

which forces the text to have right alignment and to have a format like 105.00 or 19.95 with decimal point/comma depending on user system settings. You can also add a currency sign to string format if applicable.

这会强制文本正确对齐,并根据用户系统设置使用 105.00 或 19.95 等格式,带小数点/逗号。如果适用,您还可以向字符串格式添加货币符号。

Edit: Sorry, I am spoiled with automatic importing of namespaces. In your top-level element (Usercontrol, Window, ...) add:

编辑:对不起,我被命名空间的自动导入宠坏了。在您的顶级元素(用户控件、窗口等)中添加:

<UserControl x:Class="..."
     ...
     xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"
     ...
>