wpf上的货币文本框c#

时间:2012-10-20 13:03:13

标签: c# .net wpf visual-studio-2010

我不知道如何将我的文本框从字符串转换为货币。我已经在STO上搜索了谷歌。但不明白如何使用它。

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

这是我的文本框xaml。

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

我在使用vb.net之前做过这个,但现在我使用的是wpf。仍然是wpf的新手。 任何想法怎么样?谢谢。

2 个答案:

答案 0 :(得分:5)

试试这个: -

  <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>

答案 1 :(得分:4)

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

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

有关更多格式,请参阅msdn - Custom Numeric Format

中的此链接