WPF 3.5 SP1 feature: StringFormat


One of the new features in 3.5 SP1 is stringformat. The usage is pretty simple. So following are simple snippets showing its use


<TextBox Text={Binding Path=Double, StringFormat=F3}/>


<TextBox Text={Binding Path=Double, StringFormat=Amount: {0:C}}/>


<TextBox Text={Binding Path=Double, StringFormat=Amount: \{0:C\}}/>


<TextBox>


  <TextBox.Text>


    <Binding Path=Double StringFormat={}{0:C}/>


  </TextBox.Text>


</TextBox>



 


<TextBox>


  <TextBox.Text>


    <MultiBinding StringFormat={}{0:F2} = {1:D}>


      <Binding Path=Double />


      <Binding Path=Date/>


    </MultiBinding>


  </TextBox.Text>


</TextBox>


<TextBox>


  <TextBox.Text>


    <Binding Path=Date StringFormat={}{0:MM/dd/yyyy}/>


  </TextBox.Text>


</TextBox>




<ListBox Background=Beige ItemStringFormat=F3>


  <sys:Double>1.11122</sys:Double>


  <sys:Double>2.11345</sys:Double>


</ListBox>


<GroupBox Background=AliceBlue Content={Binding Path=Double} ContentStringFormat=F4


          Header={Binding Path=Double} HeaderStringFormat=F5/>


<Label Content={Binding Path=Double} ContentStringFormat={}{0:E2}/>


<GridView>


  <GridViewColumn Header=Date


           DisplayMemberBinding={Binding XPath=Date, StringFormat=D} />



 This feature makes life a lot more easier when it comes to formatting.. So have fun with it.


Share this post

 

Comments (20)

  1. karl1406 says:

    Nice post.  Like that multi-binding example.

    I have not tried the new SP1 yet because I’m doing Silverlight and don’t want to give that up until the tools release.

    Did you try this with the decimal data type instead of double?  

    I had to write a special converter to handle the decimal data type because the "$" was blowing up the type converter for a decimal data type.  My converter strips out the "$" before the string to decimal conversion takes place.

    Did you try this with nullable properties on the data object.  I wrote a series of converters to handle nullable data types.

    Cheers,

    Karl

  2. Via this post . I won&#39;t elaborate so much about it, do read Lester&#39;s post in the link above.

  3. ASP.NET Integrated social networking in ASP.NET [Via: 100muses ] Sharepoint When to dispose of objects…

  4. Rudi Grobler says:

    I know I am a little late on blogging about .NET 3.5 SP1, but here goes… This is a huge release…

  5. Cornel says:

    In WinForms exists something related?

  6. Karl, decimals work fine and nullable values also seem fine 🙂

  7. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately

  8. Dmitrij Zaharov says:

    Seems that Binding is getting even more functionality than before 🙂

  9. My latest links about WPF (Apps, Controls, 3.5sp1 beta, HowTo, for LOB), Silverlight, XAML and URLs WPF

  10. Mijn verontschuldigingen voor de weinige posts de laatste weken. Mijn werk en leven zijn enorm de druk

  11. Jon says:

    This is good news. This is what I was looking for in the original WPF. Having to create an IValueConverter to do simple formatting was a pain. It looks like there are a few different ways to escape the {} characters?

  12. StringFormat is a new property in .Net 3.5 SP1, which is currently in Beta. See Scott’s blog for more

  13. Vous trouverez ici les principales syntaxes permettant de réaliser un Data Binding en WPF. Le but ici

  14. James Hurst says:

    Would it be too much trouble to post an example that actually works?

  15. Mes excuses pour le peu de publications au cours des dernières semaines – le travail et la vie en générale

  16. Christian says:

    Hi!

    I like the string.format feature. But there is one problem:

    If the value you are binding to is null the formated string will show

    {DependencyProperty.UnsetValue}.

    I would expect it to show simply nothing.

    Will that be fixed in the final release?

  17. tomledk says:

    Endelig! En af de nye features der kommer med WPF 3.5 SP1 er StringFormat. Lester har skrevet en fin

  18. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately

  19. I wrote some postings about data binding in the past (see here , here and here ). It is a pretty cool

  20. Via this post . I won&#39;t elaborate so much about it, do read Lester&#39;s post in the link above.

Skip to main content