Ask a questionAsk a question
 

AnswerStringFormat Binding using "%"

  • Tuesday, November 03, 2009 8:08 PMIgor Kondrasovas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,

    Currently I'm using the following code to define a simple property binding:

    <TextBlock Text="{Binding SurfaceCovered, StringFormat=F2, Mode=OneWay}"/>
    <TextBlock Text=" %"/>
    
    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.

    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

  • Wednesday, November 04, 2009 8:37 AMDutchMarcel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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/
  • Wednesday, November 04, 2009 10:05 AMZhi-Xin YeMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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!

All Replies

  • Tuesday, November 03, 2009 8:10 PMMariano O. Rodriguez Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    You should use:

    <TextBlock Text="{Binding SurfaceCovered, StringFormat=\{0:F2\}, Mode=OneWay}"/>
    


    http://weblogs.asp.net/marianor/
  • Wednesday, November 04, 2009 8:37 AMDutchMarcel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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/
  • Wednesday, November 04, 2009 10:05 AMZhi-Xin YeMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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!