简体   繁体   English

wpf上的货币文本框c#

[英]currency textbox on wpf c#

i dont know how to make my textbox from string to currency. 我不知道如何使我的文本框从字符串变为货币。 I already do search on STO, google. 我已经在STO上搜索了谷歌。 but don't understand how to use it. 但不明白如何使用它。

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. 如果我键入1000,我希望我的文本框自动更改为1,000。 if i key in 10000, my textbox will looks like 10,000. 如果我键入10000,我的文本框将看起来像10,000。 but whenever i key in 100, my textbox will still 100. 但每当我键入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. 我曾经在使用vb.net之前做过这个,但现在我正在使用wpf。 still new to wpf. 仍然是wpf的新手。 any idea how to that? 任何想法怎么样? thanks. 谢谢。

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>

Use the StringFormat dependency property to format the way you want the string to be shown on UI. 使用StringFormat依赖项属性来格式化您希望在UI上显示字符串的方式。 Try this - 尝试这个 -

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

For more formats refer to this link from msdn - Custom Numeric Format 有关更多格式,请参阅msdn中的此链接 - 自定义数字格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM