1

I'm very new in C# I tried almost evrything by searching all the web for answer, but still can't do it by myself. My question is about formating textbox

all i want is that when I writing a number in the textbox, the textbox will show this number with decimal and currency symbol.

and because i have 10 textbox that presenting numbers as currency i understand that i must do binding.

Sorry for my english.

here is my class converter

class Bindings : IvalueConverter
{
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
            if (value !=null)
            {
              double valueshop = (double)value;
              return string.Format(culture, "{0:C}", valueshop;
            }
     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
           throw new  NotImplementedException();
     }
}

this is my xaml

<window.Resources>
     <local:Bindings x:Key="ForText"/>
</window.Resources>
<StackPanel Margin="10">
<TextBox Name="MoneyOne" Text="{Binding Converter={StaticResources ForText}, Mode=OneWay, Path=valueshop, UpdateSourceTrigger=PropertyChanged, StringFormat='C'}"/>
    </StackPanel>

I hope somebody can help, thanks.

Idan Sim
  • 31
  • 1
  • 4

1 Answers1

2

You can try:

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • so what you trying to tell me that there is no option to turn regular textbox into currency textbox ( i mean, if you know how acting excel cells. If you want to display numbers as currency there is an option to change it to currency. so when you taping a number and then you exit from the cell . the cell presenting a symbol, decimal and seperator. – Idan Sim Mar 19 '18 at 12:20
  • I mean there are at least 3 options. If you want to stick with TextBox class you should take option #2 (attached properties). Other two options are about inheritance. – Yury Schkatula Mar 19 '18 at 14:31
  • hi Yury, I found here similar question [link](https://stackoverflow.com/questions/12988729/currency-textbox-on-wpf-c-sharp) , and i saw that @RohitVats answer this question by email. i think i'm didn't bind it correctly. – Idan Sim Mar 20 '18 at 08:56