locked
Textbox currency format. RRS feed

  • Question

  • I have TextBox witch accepting user input as price.

    i want when user put price it should be display this format 10000.00

    XAML:

    <TextBox Height="23" HorizontalAlignment="Left" Margin="109,284,0,0" Name="txtprice1" VerticalAlignment="Top" Width="136" RenderTransformOrigin="0.738,2.115" />

    Please guide me.

    Sunday, February 23, 2014 6:24 PM

Answers

  • hi,

    Use string format

    <TextBox Width="100" Text="{Binding Amount,Mode=TwoWay,StringFormat={}{0:C}}"/>

    • Proposed as answer by JayChase Monday, February 24, 2014 1:00 AM
    • Marked as answer by Suraj Singh Rana Monday, February 24, 2014 6:42 AM
    Sunday, February 23, 2014 7:31 PM
  • If you bind to some source property of type double or decimal and want to display two decimals, you could specify StringFormat=f2:

    <TextBox Text="{Binding YourSourceProperty,StringFormat=f2}"/>

    Otherwise you could handle the LostFocus event of the TextBox:

    <TextBox LostFocus="TextBox_LostFocus"/>

            private void TextBox_LostFocus(object sender, RoutedEventArgs e)
            {
                TextBox txtBox = sender as TextBox;
                double d;
                if (double.TryParse(txtBox.Text, out d))
                {
                    txtBox.Text = d.ToString("f2");
                }
            }


    Sunday, February 23, 2014 8:23 PM

All replies

  • hi,

    Use string format

    <TextBox Width="100" Text="{Binding Amount,Mode=TwoWay,StringFormat={}{0:C}}"/>

    • Proposed as answer by JayChase Monday, February 24, 2014 1:00 AM
    • Marked as answer by Suraj Singh Rana Monday, February 24, 2014 6:42 AM
    Sunday, February 23, 2014 7:31 PM
  • If you bind to some source property of type double or decimal and want to display two decimals, you could specify StringFormat=f2:

    <TextBox Text="{Binding YourSourceProperty,StringFormat=f2}"/>

    Otherwise you could handle the LostFocus event of the TextBox:

    <TextBox LostFocus="TextBox_LostFocus"/>

            private void TextBox_LostFocus(object sender, RoutedEventArgs e)
            {
                TextBox txtBox = sender as TextBox;
                double d;
                if (double.TryParse(txtBox.Text, out d))
                {
                    txtBox.Text = d.ToString("f2");
                }
            }


    Sunday, February 23, 2014 8:23 PM
  • Thank you very much, its work.
    Monday, February 24, 2014 6:42 AM
  • Thaaaaaaank you! For heaven's sake
    Sunday, April 7, 2019 10:28 PM