简体   繁体   中英

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.

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. Try this -

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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