Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

currency textbox on wpf c#

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.

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.

here is my textbox 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.

like image 643
Alfred Angkasa Avatar asked Jan 15 '23 06:01

Alfred Angkasa


2 Answers

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>
like image 127
Rahul Tripathi Avatar answered Jan 17 '23 21:01

Rahul Tripathi


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

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

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

like image 34
Rohit Vats Avatar answered Jan 17 '23 21:01

Rohit Vats