wpf c#上的货币文本框

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

currency textbox on wpf c#

c#.netwpfvisual-studio-2010

提问by Alfred Angkasa

i dont know how to make my textbox from string to currency. I already do search on STO, google. but don't understand how to use it.

我不知道如何让我的文本框从字符串变成货币。我已经在 STO、谷歌上搜索过了。但不明白如何使用它。

let's say i have one textbox. whenever it run the program, i key in inside the textbox. if i key in 1000, i want my textbox automatically change to 1,000. if i key in 10000, my textbox will looks like 10,000. but whenever i key in 100, my textbox will still 100.

假设我有一个文本框。每当它运行程序时,我都会在文本框中键入。如果我输入 1000,我希望我的文本框自动更改为 1,000。如果我输入 10000,我的文本框将看起来像 10,000。但是每当我输入 100 时,我的文本框仍然是 100。

here is my textbox xaml.

这是我的文本框 xaml。

<TextBox Height="25" HorizontalAlignment="Left" Margin="126,223,0,0" Name="txtPrice" 
                     VerticalAlignment="Top" Width="140" PreviewTextInput="txtPrice_PreviewTextInput" />

I ever done this before using vb.net, but now i'm using wpf. still new to wpf. any idea how to that? thanks.

我在使用 vb.net 之前曾经这样做过,但现在我正在使用 wpf。wpf 还是新手。知道怎么做吗?谢谢。

回答by Rahul Tripathi

Try this:-

尝试这个:-

  <Style TargetType="{x:Type TextBox}">
  <Setter Property="Text" Value="{SomeValue, StringFormat=C}" />
  <Style.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="Text" Value="{SomeValue, UpdateSourceTrigger=PropertyChanged}" />
    </Trigger>
   </Style.Triggers>
</Style>

回答by Rohit Vats

Use the StringFormatdependency property to format the way you want the string to be shown on UI. Try this -

使用StringFormat依赖属性来格式化您希望字符串在 UI 上的显示方式。尝试这个 -

<TextBox Text="{Binding YourBinding, StringFormat='##,#', 
                          UpdateSourceTrigger=PropertyChanged}"/>

For more formats refer to this link from msdn - Custom Numeric Format

有关更多格式,请参阅 msdn - Custom Numeric Format 中的此链接