String format in wpf

10,186

Solution 1

you could try with StringFormat="{}{0:0.####}"

This syntax with {} is due to the fact that we set a WPF property equal to a string that contains curly bracket symbols. Curly bracket symbols are interpreted by WPF in a particular way and would not be interpreted as part of the string. Without the {} the code would not compile. {} allows you to set a WPF to a string value that contains curly bracket symbols.

You can have for example a look at the link String formatting in WPF and Silverlight

Solution 2

Take a look at this link about Custom Numeric Format Strings. I think this is what you might be looking for.

Or alternatively, try this;

<TextBox Text="{Binding Position, StringFormat='{}{0:#,0000}'}"/>

Hope this helps! :)

EDIT:

This previous question might help also;

WPF binding StringFormat syntax

Share:
10,186
Hodaya Shalom
Author by

Hodaya Shalom

Updated on June 04, 2022

Comments

  • Hodaya Shalom
    Hodaya Shalom almost 2 years

    I have a TextBox on WPF that is related to the float variable in my model

    Following the TextxBox:

     <TextBox Text="{Binding Position, StringFormat=f4}"/>
    

    I want that TextBox will display a maximum 4 numbers after the point.

    So I put StringFormat=f4.

    But now, even when I have less than 4 numbers after the point and when I have a whole number it displays with 4 digits after the point.

    For example, the number 0 is shows that: 0.0000

    I want as long as it did not pass the four numbers, display it in a normal way, how can I do this?

  • Hodaya Shalom
    Hodaya Shalom almost 11 years
    Can you explain what the meaning of the line?
  • Hodaya Shalom
    Hodaya Shalom almost 11 years
    But why i see only 3 numbers ?
  • Klaus78
    Klaus78 almost 11 years
    Actually if Position is 0.12345, you should see 0.1235. At least on my PC this is what happens
  • Hodaya Shalom
    Hodaya Shalom almost 11 years
    Right!! Me too he is doing. But when I put this number: 1234.36456465445 is introduced: 1234.365