Currency text box in wpf c #

I do not know how to make a text box from a string to a currency. I am already doing a search on STO, google. but don’t understand how to use it.

let's say I have one text box. whenever it launches a program, I enter the text box. if I write 1000, I want my text box to automatically change to 1000. if I write 10,000, my text box will look like 10,000. but whenever I type 100, my text box will be 100.

here is my xaml text box.

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

I have ever done this before using vb.net, but now I use wpf. still new to wpf. any idea how is it? Thank you

+4
source share
2 answers

Use the StringFormat dependency StringFormat to format the way the string is displayed in the user interface. Try it -

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

Additional formats relate to this link from msdn - Custom Digital Format

+4
source

Try the following: -

  <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> 
+5
source

Source: https://habr.com/ru/post/1440960/


All Articles