45

I have a textbox bound to a property in an object. I have setup the string format to be p0.

However, when I enter 12 for example it is formatted as 1200% (multiplies by 100 and add % sign)

How can i set the stringformat so that for exampe 20 is formatted as 20% ?

My current control is :

<TextBox Text="{Binding Path=MyCase, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=p0}"/> 

how t change the string format so that the format for 7 is 7% not 700% ?

2 Answers 2

104
"{Binding Path=Percentage, StringFormat={}{0}%}" 
Sign up to request clarification or add additional context in comments.

5 Comments

Please take a look here: stackoverflow.com/questions/1790975/… I have the same issue, but I do want to multiply the value by 100 for the representation since I store the percentage values in a decimal col.
Note: You can add a format string to this syntax, just add a ':' followed by your format ('C', 'F6', "#.####", etc.) ex. {}{0:0.00}% for 82.345 displays 82.35% For details see: msdn.microsoft.com/en-us/library/kfsatb94.aspx msdn.microsoft.com/en-us/library/0c899ak8.aspx
In Silverlight 5 I'm getting a "Unexpected Token after end of Markup Extension." error. Any tip?
This how achieved the same result in Silverlight 5 Binding="{Binding Path=Value, StringFormat='\{0:n2\}%'}"
Thx @ShimmyWeitzhandler StringFormat={}{0:P2}
8

Another solution is to wrap the % in single quotes, and put it inside the curley brackets:

<TextBlock Text="{Binding Percentage, StringFormat={}{0:#0.00'%'}}"/> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.