.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
StringFormat Binding using "%"
StringFormat Binding using "%"
- Hello,
Currently I'm using the following code to define a simple property binding:
Now the property "SurfaceCoverd" changed and it will contain values ranging from 0.0 to 1.0, and I would like to show then to the user as from 0 % do 100 %, so I will have to multiply the value by 100 and add the " %" string.<TextBlock Text="{Binding SurfaceCovered, StringFormat=F2, Mode=OneWay}"/> <TextBlock Text=" %"/>
Is there any way to do that wiht FromatBinding in .NET 3.5 sp1, instead of using custom formater classes?
I read something about using the "%" string formatting, but I couldn't make it work.
Thanks in advance,
Igor
Project Manager at INOVATIVA Tecnologia www.inovativatec.com
Answers
- That will not multiply it by 100.This is the right format for it:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <ContentControl ContentStringFormat="{}{0:0.0%}"> <sys:Double>0.123</sys:Double> </ContentControl> </Grid> </Page>
The example is not with a binding, but you can use the same format for the StringFormat parameter of a binding.hth,Marcel
http://dutchmarcel.wordpress.com/- Marked As Answer byIgor Kondrasovas Wednesday, November 04, 2009 10:59 AM
- Hi Igor,
To display the data in percentage format, you can use the format specifier "P", P0 stands for no decimal digit, e.g. 30%; P1 stands for 1 decimal digit, e.g. 30.0%; and so on;
<TextBlock Name="textBlock1" Text="{Binding SurfaceCovered, StringFormat=P0 , Mode=OneWay}"/>
For more information about the The Percent ("P") Format Specifier, see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#PFormatString
If anything is unclear, please feel free to let me know.
Best Regards,
Zhi-Xin Ye
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer byIgor Kondrasovas Wednesday, November 04, 2009 10:59 AM
All Replies
- You should use:
<TextBlock Text="{Binding SurfaceCovered, StringFormat=\{0:F2\}, Mode=OneWay}"/>
http://weblogs.asp.net/marianor/ - That will not multiply it by 100.This is the right format for it:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <ContentControl ContentStringFormat="{}{0:0.0%}"> <sys:Double>0.123</sys:Double> </ContentControl> </Grid> </Page>
The example is not with a binding, but you can use the same format for the StringFormat parameter of a binding.hth,Marcel
http://dutchmarcel.wordpress.com/- Marked As Answer byIgor Kondrasovas Wednesday, November 04, 2009 10:59 AM
- Hi Igor,
To display the data in percentage format, you can use the format specifier "P", P0 stands for no decimal digit, e.g. 30%; P1 stands for 1 decimal digit, e.g. 30.0%; and so on;
<TextBlock Name="textBlock1" Text="{Binding SurfaceCovered, StringFormat=P0 , Mode=OneWay}"/>
For more information about the The Percent ("P") Format Specifier, see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#PFormatString
If anything is unclear, please feel free to let me know.
Best Regards,
Zhi-Xin Ye
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer byIgor Kondrasovas Wednesday, November 04, 2009 10:59 AM


